[racket] SICP/unless

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat May 19 19:23:26 EDT 2012


On May 19, 2012, at 2:45 PM, Michael Rice wrote:

> From pg. 399 in SICP:
> 
> An example that exploits lazy evaluation is the definition of a procedure unless
> 
> (define (unless condition usual-value exceptional-value)
>   (if condition exceptional-value usual-value))
> 
> ===
> 
> Huh?  For both applicative and normal order, wouldn't both exceptional-value AND usual-value be evaluated, or not, depending on condition?

SHORT: If 'unless' were defined as a CALL-BY-NAME [*] function or as a in a LAZY dialect of Racket, you would get away with this strategy.

LONG: To try it out, use the lazy language, define it like this, and run: 

> Welcome to DrRacket, version 5.3.0.8--2012-05-16(3fceae27/d) [3m].
> Language: lazy.
> > (define (unlss condition then-branch else-branch)
>       (if condition then-branch else-branch))
> > (unlss #t (+ 1 1) (/ 1 0))
> 2

See the division by 0 does not raise an error, it is never evaluated. The prompt forces the evaluation of (+ 1 1). (In a truly lazy language, it shouldn't even do that, but never mind this small quibble.) 

;; ---

If you'd like to understand how this works in the world of Racket, take a look at this section on creating languages in Racket. It is a part of a short essay I wrote a year ago or so: 

   http://www.ccs.neu.edu/home/matthias/Thoughts/Racket_is____.html#(part._.Creating_a_.Language)

;; --- 

FOOTNOTE: SICP gets it wrong when it refers to APPLICATIVE and NORMAL ORDER evaluation. Both are terms that may have been useful in the world of Lambda Calculus mathematics in the 1930s and 1940s. This whole view that the EVALUATION STRATEGY is related to Programming Language parameter passing mechanism was debunked in the 1970s by Plotkin's seminal paper on Call-by-name, call-by-value, and the Lambda Calculus [TCS 1974]. We can forgive SICP 1e for making this mistake, SICP 2e shouldn't propagate it. 

-- Matthias








-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120519/0b3e1d25/attachment.html>

Posted on the users mailing list.