Computer Science 538 Lecture Notes for 4-3-00 Lecture by Professor Fischer Notes by Michael Waddell Using the OPTION property in SML ------------------------------- Syntax: Datatype 'a option = none/some of 'a; Usage: Val Zilch = none; Val Mucho = some(10E9); However, the structure is what counts, not the keywords. Thus, the following is also legal: Datatype 'a present = absent / here of 'a; This datatype gives us an easy way to specify when data is unavailable. It also allows us to build recursive data types very simply: Example: Binary tree Datatype Bintree = Null / Leaf / Node of Bintree*Bintree; Usage: Node(Leaf,Null) ___ |___| ___/ |___| Node(Leaf,Leaf) ___ |___| ___/ \___ |___| |___| Leaf ___ |___| Null ? Here's a useful function: Fun size(Null)=0 | size(Leaf)=1 | size(Node(L,R))=size(L)+size(R)+1; How do we attach a datafield to each node of a b-tree? Syntax: Datatype 'a bintree = Null | Leaf of 'a | node of 'a bintree*'a bintree; Usage: Node(Leaf(1),Leaf(7)) ___ |___| ___/ \___ |_1_| |_7_| What if we wanted data in the internal nodes too? Syntax: Datatype 'a bintree = Null | Leaf of 'a | node of 'a*'a bintree*'a bintree; Usage: Node(5,Leaf(1),Leaf(7)) ___ |_5_| ___/ \___ |_1_| |_7_| What if I want to allow values to be null sometimes? Answer: Use OPTION! Syntax: Datatype 'a bintree = Null | Leaf of 'a option | node of 'a option*'a bintree*'a bintree; Usage: Node(Some(5),Leaf(Some(1)),Leaf(Some(7))) ___ |_5_| ___/ \___ |_1_| |_7_| Let's look at the frontier of the tree (the leaves read left->right.) Fun Frontier(Null)=[] | Frontier(Leaf(V))=[V] | Frontier(Node(T1,T2))=Frontier(T1)@Frontier(T2); Note: @ is the infix append operator.