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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

- 53 -gobble a sweet andgobble a sweet andgobble a sweet ands<strong>to</strong>pNote that eating sweets iteratively involves gobbling 1 sweet N times whereas eating sweets recursively involvesgobbling 1 sweet and then eating the remaining N-1 sweets recursively.It is useful <strong>to</strong> distinguish primitive recursion where the number of repetitions is known from general recursion herethe number of repetitions is unknown. Primitive recursion is weaker than general recursion. Primitive recursioninvolves a finite depth of function call nesting so it is equivalent <strong>to</strong> bounded repetition through iteration with a finitememory. For general recursion the nesting depth is unknown so it is equivalent <strong>to</strong> unbounded repetition throughiteration with an infinite memory.Note that imperative languages often provide repetition through recursive procedures and functions as well as throughiteration.4.3. Recursion through definitions?It might appear that our definition notation enables recursion and we could just use the name from the left of thedefinition in the expression on the right. For example, two numbers may be added <strong>to</strong>gether by repeatedly incrementingthe first and decrementing the second until the second is zero:def add x y =if iszero ythen xelse add (succ x) (pred y)Thus, <strong>to</strong> add one and two, for example:add one two => ... =>add (succ one) (pred two) => ... =>add (succ (succ one)) (pred (pred two)) => ... =>(succ (succ one)) ==threeHowever, in chapter 2 we required all names in expressions <strong>to</strong> be replaced by their definitions before theexpression is evaluated.In the above example:λx.λy.if iszero ythen xelse add (succ x) (pred y) ==λx.λy.if iszero ythen xelse((λx.λy.if iszero ythen x

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

Saved successfully!

Ooh no, something went wrong!