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.

- 199 -(tadd 7 nil) ->(7 nil nil)(tadd 4 ’(7 nil nil)) ->(7(4 nil nil)nil)(tadd 10 ’(7 (4 nil nil) nil)) ->(7(4 nil nil)(10 nil nil))(tadd 2 ’(7 (4 nil nil) (10 nil nil))) ->(7(4(2 nil nil)nil)(10 nil nil))(tadd 5 ’(7 (4 (2 nil nil) nil) (10 nil nil))) ->(7(4(2 nil nil)(5 nil nil))(10 nil nil))Hence, <strong>to</strong> add a list of numbers <strong>to</strong> an ordered binary tree:(defun taddlist (l tree)(if (null l)tree(taddlist (cdr l) (tadd (car l) tree))))Finally, <strong>to</strong> traverse a binary tree in ascending order:(defun traverse (tree)(if (null tree)nil(append (traverse (left tree))(cons (item tree) (traverse (right tree))))))For example:(traverse ’(7 (4 (2 nil nil) (5 nil nil) (10 nil nil))) ->(2 4 5 7 10)10.15. Dynamic and lexical scopeLISP is often presented as if it were based on the λ calculus but this is somewhat misleading. LISP functionabstraction uses a notation similar <strong>to</strong> the λ abstraction but the relationship between bound variables and variables inexpressions is rather opaque.In our presentation of λ calculus, names have lexical or static scope. That is, a name in an expression corresponds <strong>to</strong>the bound variable of the innermost enclosing function <strong>to</strong> define it.

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

Saved successfully!

Ooh no, something went wrong!