Skip to content

Instantly share code, notes, and snippets.

@aldwyish
Created November 20, 2011 22:25
Show Gist options
  • Select an option

  • Save aldwyish/1381052 to your computer and use it in GitHub Desktop.

Select an option

Save aldwyish/1381052 to your computer and use it in GitHub Desktop.
sampleexam10 Q6
-- | 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
-- | 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