[racket] Quoted expressions in #lang racket
On Sep 18, 2011, at 4:41 PM, Racket Noob wrote:
> Ok, maybe this is not something that's important in other programming languages, but it *is* important in lisps. As a lisp educator, how can you *not* to teach this fundamental fact about lisp?
Two questions:
1) How many people here are Lisp educators? Most of us seldom if ever use Lisp, but use Racket every day.
2) Is "representing expressions as lists" really a fundamental fact about Lisp, or is it an implementation decision taken by some of the early implementers of Lisp?
Actually, in many ways I sympathize: when I first encountered Racket (which was then called PLT Scheme), I took a similar interpreter-based approach to understanding what it was doing (having previously encountered Lisp and learned the same "expressions are lists" dictum you learned). It turns out not as helpful as one would think.
In fact, the Racket reader does NOT represent expressions as lists; it represents them as "syntax objects", which (as I understand it) can be nested in the same way lists can, but carry some additional information. So when you type the eight characters "'(1 2 3)", what happens?
1) The reader converts this into a syntax object containing "quote" and a nested syntax object containing "1", "2", and "3".
2) The evaluator converts this into an internal representation of a list containing 1, 2, and 3
3) The printer converts this into the eight characters "'(1 2 3)"
> I don't understand why you (i.e. Racket implementers) choose Racket by default prints list this way.
In part because of our experience in the classroom. For beginners, it's convenient for the read-eval-print sequence to be idempotent, i.e. if you do it twice, you get the same result as if you had done it once. In other words, anything you get as an answer to a question can be copied and pasted back in, and you'll get it again. And you're right, that was NOT true of classic Lisp. Suppose the variable y has the value (list '+ 3 4); then if I type the two characters "'y" into a REPL, I get the one character "y"; if I type the one character "y" into the REPL, I get the seven characters "(+ 3 4)"; and if I type this into the REPL, I get the one character "7".
In our experience, this confuses students, so we use an analogy more like arithmetic or algebra. Don't think of the operation as "evaluating" but rather "simplifying as far as possible." In grade-school arithmetic, suppose x has the value 5, and I want to simplify "3 + 4x". It simplifies to 3 + 4*5, which simplifies to 3 + 20, which simplifies to 23, which doesn't simplify any farther. We've seen four different expressions along the way, but they all simplify to 23.
Now let's do that in Racket. Suppose variable x has the value 5, and I type
(+ 3 (* 4 x))
This is simplified to (+ 3 (* 4 5)), which is then simplified to (+ 3 20), which is then simplified to 23, which can't be simplified any farther. I can type any of these four expressions into a REPL, and I get the exact same result.
Now let's try this with lists, the way DrRacket does it by default. I type in
(cons 1 (list 2 3 x))
which is simplified to (cons 1 (list 2 3 5)), which is then simplified to (list 1 2 3 5) (or, if you prefer, (cons 1 (cons 2 (cons 3 (cons 5 empty)))), which can't be simplified any farther. I can type any of these four expressions into a REPL, and I get the exact same result.
Now let's try it the way you're thinking about it. I type in
(cons 1 (list 2 3 x))
which is read as (list 'cons 1 (list 'list 2 3 'x)), which after one step of reduction produces (list 'cons 1 (list 'list 2 3 5)), which after another step of reduction produces (list 'cons 1 (list 2 3 5)), which after another step of reduction produces (list 1 2 3 5), which you want to print out as (1 2 3 5). We've seen six different expressions. Typing these six different expressions into a REPL gives you five different answers, one of which is an error message.
Does that help?
Stephen Bloch
sbloch at adelphi.edu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110918/c082ccc9/attachment.html>