; ; examples from 314 lecture ; ;first let's define a function of 2 arguments ; (define (f x y) (+ x y)) ; ; then let's think of forming an increment-by-one function from f ; we can do this because we can "partially evaluate" f on only one argument ; thus (f 1 z) is that function which when giving an argument, returns ; that argument incremented by 1 ; thus (f 1 z) is a function whose value is a function here ; (define (g z) (f 1 z)) ; ; we can do the same effect with the "let" command ; let makes the name "x" and the value "2" be the same within ; the expression that is the second argument of the let ; (define (h z) (let ((x 2)) (f x z))) ; ; now say we really want to define a family of increment by a constant ; functions. how could we do that? ; here we use a lambda expression to build the function increment-by-z ; so when we call j with an argument 3, we actually obtain the function ; increment by 3. then we can apply (j 3) to another argument w ; and obtain the value of w+3. ; ; i.e., (j 3) --- returns a function of one variable "x+3" which can be ; applied to an argument ; ((j 3) 2) ---returns 5 ; (define (j z) (lambda (x) (f x z))) ;here is the results of running these functions; of course you don't see the ; leading semicolons at your workstation ;> (load "higher-order-functions.scm"); using load-noisily set to on shows ;f us the function names as they load ;g ;h ;j ;> (pp f) ; pretty prints the function f just loaded ;(lambda (x y) (+ x y)) ;> (f 1 2) returns 3 ;> (f -1 1) returns 0 ;> (pp g) ;(lambda (z) (f 1 z)) ;> (g 3) returns 4 ;> (pp h) ;(lambda (z) (let ((x 2)) (f x z))) ;> (h 3) returns 5 ;> (h 6) returns 8 ;> (pp j) ;(lambda (z) (lambda (x) (f x z))) ;> (j 1) ;#[compound value] ;this is how you see a function value ;> ((j 1) 3) returns 4 ;this makes sense since (j 1) returns a function value ;> ((j 3) 4) returns 7 ;> ((j 5) 0) returns 5 ;> ((j 1)0) returns 1