10.07.2015 Views

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

- 191 -(> 9 8 7 6 5) ->tThe primitive:numberpreturns true if its argument is a number. For example,(numberp 42) ->t10.6. <strong>Lambda</strong> functionsLISP uses a notation like that for the λ calculus <strong>to</strong> define nameless functions.It is important <strong>to</strong> note that LISP functions do not have all the properties we might expect from the λ calculus. Inparticular, special techniques are needed <strong>to</strong> pass functions as arguments, <strong>to</strong> return functions as values and <strong>to</strong> applyfunctions returned as values <strong>to</strong> new arguments.Functions are defined as forms with the primitive:lambdafollowed by a flat list of bound variables and the body form:where:(lambda () ) ::= | For example, <strong>to</strong> square a number:(lambda (x) (* x x))or <strong>to</strong> find the sum of the squares of two numbers:(lambda (x y) (+ (* x x) (* y y)))or <strong>to</strong> find the value of the quadratic:ax 2 +bx+cgiven a, b, c and x:(lambda (a b c x) (+ (* a (* x x)) (* b x) c)))Note that functions are normally uncurried in LISP.Note that LISP systems will reject attempts <strong>to</strong> present lambda functions directly as values other than in the functionposition in a form. lambda is not a primitive which denotes a system function. Instead it acts as a marker <strong>to</strong> indicatea function form. However, if a LISP system sees a naked lambda function form it will try <strong>to</strong> find a function associatedwith lambda and fail. The special techniques needed <strong>to</strong> manipulate function values are discussed below.A function is applied <strong>to</strong> arguments in a form with the function followed by the arguments. The function’s body isevaluated with the bound variables associated initially with the corresponding arguments:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!