Hacker News new | ask | show | jobs
by colanderman 5434 days ago
I looked at it and thought, "easy, I'll just rewrite the Scheme function in Mercury and tell the compiler to memoize the function."

Of course, obviously the point of the exercise is to encourage thinking about how a computer actually performs computations, which is in a linear, imperative fashion. In that case, why use Scheme in the first place? Its syntax and semantics encourage exactly the opposite style of programming, and in my opinion, obstruct the learning process in this kind of problem.

To make an analogy, teaching this problem using Scheme is like teaching someone how to bake a cake using an unplugged Kitchenaid. The Kitchenaid would work great if it was plugged in, but it's not, so you have to push the beater around by hand. Why not just use a hand beater in the first place? [Scheme would work great for this problem if it had memoization, but it doesn't, so you have to solve the problem as if you were using Python. Why not just use Python in the first place?]

1 comments

The point of Scheme isn't to provide you with a given feature like memoization, its to provide you with the tools so you can do it yourself. SICP covers memoization at the end chapter 3.3.3 (http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-22.html...):

  (define (memoize f)
    (let ((table (make-table)))
      (lambda (x)
        (let ((previously-computed-result (lookup x table)))
          (or previously-computed-result
              (let ((result (f x)))
                (insert! x result table)
                result))))))
And SICP quits at this point? What if "previously-computed-result" is false, because f computes boolean values? This is just an 80% solution, which works for "fib".
The implementation of tables that he uses isn't actually reasonable for most use. If actually implementing memoization, one would probably use SFRI-44 style maps, which come with a 'contains-key' function that solves this problem.
The same could be said of any Turing-complete language (though I'll admit Scheme's macro system gives it a leg up when it comes to implementing new features.) But given that Scheme's syntax encourages thinking in terms of mathematical recursion, it seems silly that (a) I can't write something so simple as the Fibonacci function by using its traditional mathematical recursive definition, and that (b) SICP encourages the use of Scheme to solve problems which require thinking in terms of iteration and mutation -- this is a pedagogical faux pas.
The difference between Scheme and any "Turing complete language", is the philosophy behind it, which is one of minimalism. That's why memoization isn't implemented for you.

Scheme was also designed to encourage implementing non-recursive control structures, as well as programming with side effects. Scheme isn't supposed to be totally pure. If you encounter a problem that requires mutation, you're supposed to try to abstract away from the mutation, if possible, and it gives you the tools to do that. (first class functions, macros, continuations, etc. are all very effective tools for abstraction, once you learn them.)

I recommend that you use SICP to learn Scheme, as it will make more sense when you actually know how to use it. It's an incredibly elegant and beautiful language, and it's certainly still relevant.

P.S. If you're thinking of those problems in terms of iteration and mutation, you're probably doing it wrong :). Think more functionally.

>I recommend that you use SICP to learn Scheme, as it will make more sense when you actually know how to use it.

I used it my entire graduate career, under an advisor who is part of the inner circle of PLT/Racket. I'm quite familiar with it.

> P.S. If you're thinking of those problems in terms of iteration and mutation, you're probably doing it wrong :). Think more functionally.

I primarily program in Mercury and Coq (languages which are both purely functional). I can promise you I have no problem "thinking functionally". The solution presented in SICP is not structurally recursive (while the "naïve" solution is) but rather relies on an accumulator, tail calls, and a measure to terminate recursion... this is much closer in spirit to an iterative algorithm than a recursive one.