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.

- 134 -[7,[4,[3,EMPTY,EMPTY],EMPTY],[9,EMPTY,EMPTY]]To add 5 <strong>to</strong> this tree:TADD 5 [7,[4,[3,EMPTY,EMPTY],EMPTY],[9,EMPTY,EMPTY]]5 comes before 7 so it is added <strong>to</strong> the left sub-tree:[7,(TADD 5 [4,[3,EMPTY,EMPTY],EMPTY]),[9,EMPTY,EMPTY]]Now, 5 comes after 4 so it is added <strong>to</strong> the right sub-tree:[7,[4,[3,EMPTY,EMPTY],(TADD 5 EMPTY)],[9,EMPTY,EMPTY]] -> ... ->[7,[4,[3,EMPTY,EMPTY],[5,EMPTY,EMPTY]],[9,EMPTY,EMPTY]]To add an arbitrary list of numbers <strong>to</strong> a tree, if the list is empty then return the tree. Otherwise add the tail of the list <strong>to</strong>the result of adding the head of the list <strong>to</strong> the tree:Thus:rec TADDLIST [] TREE = TREEor TADDLIST (H::T) TREE = TADDLIST T (TADD H TREE)TADDLIST [7,4,9,3,5,11,6,8] EMPTY -> ... ->[7,[4,

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

Saved successfully!

Ooh no, something went wrong!