Browse Source

text of exercises instructions, and code for the first exercise

john melesky 8 years ago
parent
commit
e7ec335e3c
2 changed files with 75 additions and 0 deletions
  1. 37 0
      chapter1/exercises.sml
  2. 38 0
      chapter1/exercises.txt

+ 37 - 0
chapter1/exercises.sml

@@ -0,0 +1,37 @@
+type key = string
+datatype tree = LEAF | TREE of tree * key * tree
+
+val empty = LEAF
+
+fun insert (key, LEAF) = TREE(LEAF, key, LEAF)
+  | insert (key, TREE(l,k,r)) =
+      if key < k
+        then TREE(insert(key,l),k,r)
+      else if key > k
+        then TREE(l,k,insert(key,r))
+      else TREE(l,key,r)
+
+
+fun member key LEAF = false
+  | member key (TREE(l,k,r)) =
+    if key = k
+    then true
+    else (member key l) orelse (member key r)
+
+(* This is not exhaustive, and will fail if the tree 
+   was not created using `insert` *)
+fun fastermember key LEAF = false
+  | fastermember key (TREE(l,k,r)) =
+    if key = k
+    then true
+    else if key < k
+    then fastermember key l
+    else fastermember key r
+
+
+
+
+
+
+
+

+ 38 - 0
chapter1/exercises.txt

@@ -0,0 +1,38 @@
+1.1 This simple program implements persistent functional binary search
+trees, so that if tree2-insert(x,tree1), then tree1 is till available
+for lookups even while tree2 can be used.
+
+type key = string
+datatype tree = LEAF | TREE of tree * key * tree
+
+val empty = LEAF
+
+fun insert (key, LEAF) = TREE(LEAF, key, LEAF)
+  | insert (key, TREE(l,k,r)) =
+      if key < k
+        then TREE(insert(key,l),k,r)
+      else if key > k
+        then TREE(l,k,insert(key,r))
+      else TREE(l,key,r)
+
+a. implement a `member` function that returns `true` if the item is
+found, else `false`
+
+b. Extend the program to include not just membership, but the mapping
+of keys to bindings:
+
+datatype 'a tree = ...
+insert: 'a tree * key * 'a -> 'a tree
+lookup: 'a tree * key -> 'a
+
+c. These trees are not balanced; demonstrate the behavior on the
+following two sequences of insertions:
+   (a) t s p i p f b s t
+   (b) a b c d e f g h i
+
+d*. Research balanced search trees in Sedgewick [1997] and recommend a
+balanced-tree data structure for functional symbol tables. Hint: To
+preserve a functional style, the algorithm should be one that
+rebalances on insertion but not on lookup, so a data structure such as
+splay trees is not appropriate.
+