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.

- 136 -(7::(APPEND (TRAVERSE EMPTY)(9::(TRAVERSE EMPTY)))) -> ... ->APPEND (APPEND (APPEND [](3::[]))(4::(APPEND [](5::[])))(7::(APPEND [](9::[]))) -> ... ->APPEND (APPEND [3](4::[5]))(7::[9]) -> ... ->APPEND [3,4,5] [7,9] -> ... ->[3,4,5,7,9]7.12. Binary tree searchOnce a binary tree has been constructed it may be searched <strong>to</strong> find out whether or not it contains a value. The searchalgorithm is very similar <strong>to</strong> the addition algorithm above. If the tree is empty then the search fails:TFIND V EMPTY = FALSEIf the tree is not empty, and the required value is the node value then the search succeeds:TFIND V (NODE NV L R) = TRUE if V NVOtherwise, if the requiredvalue comes before the node value then try the left branch:TFIND V (NODE NV L R) = TFIND V L if V NVOtherwise, try the right branch:TFIND V (NODE NV L R) = TFIND V R if NOT ( V NV)For example, for a binary integer tree:rec TFIND V EMPTY = ""or TFIND V [NV,L,R] =IF EQUAL V NVTHEN TRUEELSEIF LESS V NVTHEN TFIND V LELSE TFIND V RFor example:TFIND 5 [7,[4,[3,EMPTY,EMPTY],[5,EMPTY,EMPTY]],[9,EMPTY,EMPTY]] -> ... ->

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

Saved successfully!

Ooh no, something went wrong!