[plt-scheme] Teaching Scheme
On May 4, Samuel Williams wrote:
>
> It would also be great if someone could rewrite the Scheme source
> code example so that it is as close as possible to the C
> implementation:
>
> http://programming.dojo.net.nz/languages/c/index
>
> I understand that Scheme is very different from C, and uses
> different paradigms, so it doesn't have to be identical, however it
> would be great if it worked in generally the same way. It would be
> great if you could include comments explaining how it works and what
> is happening (like the C example).
Since I didn't see any actual reply that had that, here's a direct
translation of the C code, and it's as documented as the C code (the
only difference is that it returns the list open open doors rather
than print them all):
#lang scheme/base
(define (main)
;; Initialize the array of doors to 0 (closed)
(define doors (make-vector 100 #f))
;; Process the doors
(for* ([pass (in-range 100)]
[door (in-range pass 100 (+ pass 1))])
(vector-set! doors door (not (vector-ref doors door))))
;; Return a list of open doors
(for/list ([(door i) (in-indexed (in-vector doors))] #:when door)
(+ i 1)))
(main)
And just to demonstrate the nonsensical categorization of compiled/
interpreted or the paradigm classification -- here are two shorter
programs that produce the same result using very different approaches
for a solution:
#lang s-exp framework/private/decode
00jLL9LPySwuUdDQyFTQyMzTLUrMS09VMFQwNNTU1FTQ0FLIVMjU1AQA
#lang s-exp framework/private/decode
0ygszS9JVdAwVDBRsFQwNFMwMlUwNlMwsVQwM1GwMFQwNDDQ1AQA
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://barzilay.org/ Maze is Life!