[BULK] [plt-scheme] Teaching Scheme

From: Shriram Krishnamurthi (sk at cs.brown.edu)
Date: Mon May 3 10:31:52 EDT 2010

Functions compute values.  They don't print output.

On Mon, May 3, 2010 at 7:26 AM, Samuel Williams
<space.ship.traveller at gmail.com> wrote:
> Dear Stephen,
> Can you let me know firstly which function is responsible for printing the
> output?
> Secondly, can you add comments explaining how it works? I can generally
> appreciate what you are doing, but I'm not a Scheme programmer so I might
> not understand the exact detail correctly.
> Thanks
> Samuel
> On 4/05/2010, at 1:01 AM, Stephen Bloch wrote:
>
> Well, that particular algorithm relies very heavily on mutation, but here's
> a functional version.
> (define (door-open door)
>   (doh door 99))
> (define (doh door pass)
>   (cond [(zero? pass) true]
>         [(= (remainder door (+ pass 1)) pass)
>          (not (doh door (- pass 1)))]
>         [else
>          (doh door (- pass 1))]))
> (define doors (build-list 100 door-open))
>
>
> Somewhat shorter but more cryptic:
> (define (door-open door)
>   (doh door 99))
> (define (doh door pass)
>   (or (zero? pass)
>       (not (boolean=? (= (remainder door (+ pass 1)) pass)
>                       (doh door (- pass 1))))))
> (define doors (build-list 100 door-open))
>
>
> Even shorter, but takes some math (and illustrates how artificial this
> problem is :-) :
> (define (door-open door)
>    (integer? (sqrt (+ door 1))))
> (define doors (build-list 100 door-open))
>
>
> Stephen Bloch
> sbloch at adelphi.edu
>
>
>
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>


Posted on the users mailing list.