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.

- 137 -TFIND 5 [4,[3,EMPTY,EMPTY],[5,EMPTY,EMPTY]] -> ... ->TFIND 5 [5,EMPTY,EMPTY] -> ... ->TRUEFor example:TFIND 2 [7,[4,[3,EMPTY,EMPTY],[5,EMPTY,EMPTY]],[9,EMPTY,EMPTY]] -> ... ->TFIND 2 [4,[3,EMPTY,EMPTY],[5,EMPTY,EMPTY]] -> ... ->TFIND 2 [3,EMPTY,EMPTY] -> ... ->TFIND 2 EMPTY -> ... ->FALSE7.13. Binary trees of composite valuesBinary trees, like linear lists, may be used <strong>to</strong> represent ordered sequences of composite values. Each node holds onecomposite value from the sequence and the ordering is determined by one sub-value.The tree addition functions above may be modified <strong>to</strong> work with composite values. For example, we might hold thecirculation list of names in a binary tree in surname order. Adding a new name <strong>to</strong> the tree involves comparing the newsurname with the node surnames:rec CTADD N EMPTY = [N,EMPTY,EMPTY]or CTADD [F,S] [[NF,NS],L,R] =IF STRING_LESS S NSTHEN [[NF,NS],(CTADD [F,S] L),R]ELSE [[NF,NS],L,(CTADD [F,S] R)]rec CTADDLIST [] TREE = TREEor CTADDLIST (H::T) TREE = CTADDLIST T (CTADD H TREE)For example:CTADDLIST[["Mark","Monkey"],["Graham","Goat"],["Quentin","Quail"],["James","Jaguar"],["David","Duck]] EMPTY -> ... ->

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

Saved successfully!

Ooh no, something went wrong!