CS135 Lecture : Intro to Computer Science Introduction to functional programming in scheme, defining constants and functions
Document Summary
Our definitions f(x) = x2,g(x,y)=x+y become: (define (fx) (* x x)) (define (g x y (+ x y)) Define is a special form (looks like a scheme function, but not all of its arguments are evaluated). It binds a name to an expression (which uses the parameters that follow the name). The body expression typically uses the parameters together with other built-in and user- defined functions. Replace the parameters with values in order to call function (i. e (g 3 5) will result in 8) Each parameter name has meaning only within the body of its function: (define (fx) (* x x)) (define (g x y (+ x y)) There will be no confusion between the x values. Kind of like a local variable in java. The definitions k = 3 and p = k2 become: (define k 3) binds 3 to the function k (define p (* k k)) binds the product of (k *k) to p .