[plt-scheme] Why do layman programmers care about Currying?
Below is some code I've just written that uses a heavily curried
style. I probably won't have written it in such a form if this thread
hadn't been active, but as it is I think it gives a good example how
currying can be advantageous. The code is defining a bunch of
functions that can be used to generate observations for a simulated
robot as it moves about a map.
It's completely untested.
HTH,
N.
;; The map, a simplified CS department floor:
;;
;; A -- D -- E -- H
;; | | | |
;; B -- C F -- G
(define (make-generator)
(vector->pseudo-random-generator #(1 1 1 1 1 1)))
(define noise 0.2)
;; (psuedo-random-generator -> (U 0 1) -> -> (U 0 1))
(define (make-wall generator)
(lambda (real)
(define false (if (zero? real) 1 0))
(lambda ()
(define real? (if (< (random generator) noise)
#f
#t))
(if real? real false))))
;; (psuedo-random-generator -> (U 0 1) (U 0 1) (U 0 1) (U 0 1) -> -> (U 0 1))
(define (make-place generator)
(define wall-maker (make-wall generator))
(lambda (n e s w)
(define north (wall-maker n))
(define east (wall-maker e))
(define south (wall-maker s))
(define west (wall-maker w))
(lambda ()
(vector (north) (east) (south) (west)))))
(define generator (make-generator))
(define place-maker (make-place generator))
(define a (place-maker 1 0 0 1))
(define b (place-maker 0 0 1 1))
(define c (place-maker 0 1 1 0))
(define d (place-maker 1 0 0 0))
(define e (place-maker 1 0 0 0))
(define f (place-maker 0 0 1 1))
(define g (place-maker 0 1 1 0))
(define h (place-maker 1 1 0 0))