CS152
Chris Pollett
Nov 17, 2021
(define double (lambda (x) (+ x x))) (double (* 3 4)) =>(+ (* 3 4) (* 3 4)) =>(+ 12 (* 3 4)) =>(+ 12 12) ; we compute (* 3 4) twice =>24
(define f (lambda () (let ((done #f) (memo '()) ; memo initially unset (code (lambda () (* 3 4)))) (if done memo ; if memo is set, return it (begin (set! memo (code)) ; remember value (set! done #t) ; note that we set it memo))))) ; and return it
=>(+ (f) (f)) =>(+ 12 (f)) ; first call computes value of (*3 4) =>(+ 12 12) ; second call uses remembered value =>24
void p(int x, int y) { x++; y++; } int main() { int a = 1; p(a,a); ... }The above would have value 3 under Pass By Reference, value 2 under Pass By Value Result.
(define output (my_prog input))where we imagine output and input are streams.
(define driver (lambda (s) (if (null? s) '() ; nothing left (begin (display (car s)) (driver (cdr s)))))) (driver output)
(define squares (lambda (s) (cons-stream "please enter a number\n" (let ((n (head s))) ; head, tail, and cons-stream ; for streams from last day (if (eof-object? n) '() (cons-stream (* n n) (cons-stream #\newline (squares (tail s))))))))) (define output (squares input)))