#lang scheme ; NOTE: you must use the language level 'module', ; rather than advanced-student, to get the var-args syntax: (define (test-language-level a b . otherArgs) (length otherArgs)) ;;; We implement a random-number generator. ;;; We need state to do this; ;;; version 1 will use (and set!) a global variable `seed`. ;;; (define seed 1) (define MAX-RAND 123) (define (next-rand-v1) (begin (set! seed (remainder (+ (* 23 seed) 17) MAX-RAND)) seed)) (define (set-seed!-v1 new-val) (set! seed new-val)) "v1:" (next-rand-v1) (next-rand-v1) (next-rand-v1) (set-seed!-v1 1) (next-rand-v1) (next-rand-v1) ;;;;;;;;;;;;;;;; ;; A version with 'seed' being 'private'. (define two-rng-funcs (let* {[seed 1] [MAX-RAND 123] [next-rand (lambda () (begin (set! seed (remainder (+ (* 23 seed) 17) MAX-RAND)) seed))] [set-seed! (lambda (new-val) (set! seed new-val))]} (list next-rand set-seed!))) (define next-rand-v2 (first two-rng-funcs)) (define set-seed!-v2 (second two-rng-funcs)) ;;; Note: the above is common enough that scheme provides 'match-define': ;;; (match-define (list next-rand-v2 set-seed!-v2) two-rng-funcs) ;;; DEFINITION: the "closure" of a function is the set of all ;;; binding which the function can refer to. ;;; Note that at the top-level, ;;; the id `next-rand-v2` is in scope, ;;; the id `seed` is not in scope, ;;; but it *is* in the function's scope. ;;; (Put another way: even though we finished eval'ing the let* ;;; a long time ago, the variable it created might live on inside ;;; a function's closure, so it can't be garbage collected. ;;; Hopefully such variables were allocated on the heap, ;;; not on the stack!) "v2:" (next-rand-v2) (next-rand-v2) (next-rand-v2) (set-seed!-v2 1) (next-rand-v2) (next-rand-v2) ;;;;;;;;;;;;;; ;; A version where we can make ;; *multiple* pairs-of-functions-which-each-share-a-local-`seed`. (define (rng-factory) (let* {[sseed 1] [MAX-RAND 123] [next-rand (lambda () (begin (set! sseed (remainder (+ (* 23 sseed) 17) MAX-RAND)) sseed))] [set-seed! (lambda (new-val) (set! sseed new-val))]} (list next-rand set-seed!))) ;; The only difference in code between v2 and v3: ;; the parens around 'rng-factory'! (match-define (list next-rand-v3a set-seed!-v3a) (rng-factory)) (match-define (list next-rand-v3b set-seed!-v3b) (rng-factory)) "v3a:" (next-rand-v3a) (next-rand-v3a) (next-rand-v3a) (set-seed!-v3a 1) (next-rand-v3a) "v3b:" (next-rand-v3b) (next-rand-v3b) (next-rand-v3b) (set-seed!-v3b 1) (next-rand-v3b) (next-rand-v3b) "continue using next-rand-v3a" (next-rand-v3a) (next-rand-v3a) ;;;;;;;;;;;;;;;;;; ;; A version where instead of returning a list-of-functions, ;; we return one "meta function" which dispatches to the ;; function that is being asked for: ;; (define (new-rng) (let* {[sseed 1] [MAX-RAND 123] [next-rand (lambda () (begin (set! sseed (remainder (+ (* 23 sseed) 17) MAX-RAND)) sseed))] [set-seed! (lambda (new-val) (set! sseed new-val))] } (lambda (msg . other-args) (cond [(symbol=? msg 'next) (apply next-rand other-args)] [(symbol=? msg 'seed!) (apply set-seed! other-args)] [else (error 'rng (format "No such method recognized: ~a" msg))])))) "v4a: (objects)" (define r (new-rng)) (define s (new-rng)) (r 'next) (r 'next) (r 'next) (r 'seed! 1) (r 'next) (r 'next) "v4b:" (s 'next) (s 'next) (s 'next) (s 'seed! 1) (s 'next) (s 'next) ;;;;;;;;;;;;;;;; ;;; A sub-class: ;;; "class niftier-rng extends rng": ;;; ;;; We add a new method 'skip' (which always returns 43), ;;; and we override 'next' so that it always resets the seed to 17: (define (new-niftier-rng) (let* {[super (new-rng)] ; The superclass object. [name "hello"]} ; A new field, only in the subclass. (lambda (msg . other-args) (cond [(symbol=? msg 'skip) 43] [(symbol=? msg 'next) (begin (super 'seed! 17) (super 'next))] #;[(symbol=? msg 'get-seed) sseed] ; This is what we *want* to return, but it'd be an error: sseed ; is in super's scope, but not ours! ; Our approach to implementing an object system can do most things, ; but it can't emulate Java's 'protected' access. [else (apply super msg other-args)])))) ;;; Exercise: our methodology for faking objects (by using closures and ;;; a 'dispatcher' function) does allow for calling superclass methods ;;; (through a variable we conveniently named `super`). ;;; However, how could we call methods in the *same* class? ;;; Hint: include the dispatcher method inside the let*, ;;; perhaps naming it `this`. ;;; But `let*` won't quite work; you'll need `letrec`! "v5 (subclassing)" (define ss (new-niftier-rng)) (ss 'next) (ss 'next) (ss 'skip) (ss 'next) (ss 'seed! 1) (ss 'next) ;;; Overall lesson: ;;; The sandwiching of 'lambda' and 'let' is doing interesting ;;; things for us.