Created
November 20, 2011 22:25
-
-
Save aldwyish/1381052 to your computer and use it in GitHub Desktop.
sampleexam10 Q6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- | a traversal that only return the concatenation of all the lists in the tree | |
| module Exam where | |
| data Ttree a = Nil | Node3 a (Ttree a) (Ttree a) (Ttree a) | |
| ttree= (Node3 [0] (Node3 [1,11] Nil Nil Nil) (Node3 [2,21] Nil Nil Nil) (Node3 [3,31] Nil Nil Nil)) | |
| concate_tree:: Ttree [a] -> [a] | |
| concate_tree t = concate_tree' t [] | |
| concate_tree':: Ttree [a] -> [a] -> [a] | |
| concate_tree' Nil acc = acc | |
| concate_tree' (Node3 t r m l) acc = | |
| let | |
| rest = concate_tree' l (concate_tree' m (concate_tree' r acc)) | |
| in | |
| t ++ rest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- | a traversal that find the shortest list in the tree and return the concatenation of all the lists in the tree | |
| module Exam where | |
| data Ttree a = Nil | Node3 a (Ttree a) (Ttree a) (Ttree a) | |
| -- | data holder | |
| type TreeAcc a = (a,a) | |
| -- | simple test case | |
| ttree= (Node3 [0] (Node3 [1,11] Nil Nil Nil) (Node3 [2,21] Nil Nil Nil) (Node3 [3,31] Nil Nil Nil)) | |
| concate_tree:: Ttree [a] -> TreeAcc [a] | |
| concate_tree tree@(Node3 t _ _ _) = concate_tree' tree (t,[]) | |
| concate_tree':: Ttree [a] -> TreeAcc [a] -> TreeAcc [a] | |
| concate_tree' Nil tacc = tacc | |
| concate_tree' (Node3 t r m l) tacc = | |
| let | |
| (s ,rest) = concate_tree' r (concate_tree' m (concate_tree' l tacc)) | |
| shortest = if (length t) < (length s) then t else s | |
| in | |
| (shortest,t ++ rest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment