You are on page 1of 3

Introduction to CS135 f(x,y) = x + y Name: f Parameters: eg. x, y Algebraic expressions using the parameters: eg. x + y Application of a function: eg.

f(1, 3) Evaluate innermost first, left to right.

Scheme g(1, 3) becomes (g 1 3) g( g(1,3),f(2)becomes (g (g 1 3) (f 2)) 3 2 + 4/5 becomes (+ ( - 3 2) (/ 4 5 )) (6-4)(3+2) becomes (* (- 6 4) (+ 3 2)) *extra brackets are harmful in Scheme. Brackets imply a function. Do not write (( + 2 2)) (10 + 4) * 3 / (15-5) becomes (/ ( * ( + 10 4) 3) (- 15 5))

Dr. Racket Definitions and interactions window Interactions (REPL: read, evaluate, print, loop): interpreted, not complied Ctrl + up arrow: repeat last line you typed 2^6 = 64 2^10 = 1024 Can store exact rational numbers (eg. 1/3), but not irrational (eg. sqrt 2) Integers in Scheme are unbounded Expressions whose values are not rational are flagged as being inexact (#) (5 * 14) infix notation (operation is in between the operands), Scheme is prefix Syntax errors: structure is wrong. Eg. (* + 3 4 2) (/ 25 0) not a syntax error, mathematical error

Defining functions

A function consists of: name, list of parameters, a single body expression. f(x) = x^2, g(x, y) = x + y become (dene (f x) ( x x)) (dene (g x y) (+ x y) define is a special form (not a function because all of its arguments are not evaluated). It binds a name to an expression (which uses the parameters that follow the name) Each parameter name has meaning only within the body of its function. The two uses of x are independent (define (h y z) (f (g y,z))) h(y, z) = f( g(y,z))

(g (g 1 3) (f 2)) (g ( + 1 3) (f 2)) substitute (g 4 ( f 2)) ( g 4 (* 2 2)) (g 4 4 ) (+ 4 4) 8 simplify substitute simplify substitute simplify

There are many ways to write the same function. The names of the parameters are meaningless. Constants (define k 3) (define p (* k k)) p=9 (define x 3) (define (f x y) (- x y)) (+ x x ) = 6 (f 5 x) = (f 5 3) = (- 5 3) = 2

(f 3 2) = (- 3 2) = 1 Can give meaningful names to useful values Constants can be used in any expression

You might also like