I’m needing some help in figuring out how to create a leftSpine function in Haskell.
Basically, it is supposed to take all the left most leafs and put them into a list, but whenever I run my code I get an empty list. Any help would be appreciated.
Here is my code.
data Tree x = Leaf | Node (Tree x) x (Tree x)
deriving Show
leftSpine :: Tree x -> [x]
leftSpine Leaf = []
leftSpine (Node lt x rt) = (leftSpine lt)
And here is my code to test it.
leftSpine (Node (Node (Node Leaf 1 Leaf) 2 (Node Leaf 3 Leaf))
4
(Node (Node Leaf 5 Leaf) 6 (Node Leaf 7 Leaf)))
It should equal [4,2,1] but it just comes up as [].
You weren’t actually putting anything into the list. The difference is the
x:leftSpine ltinstead ofleftSpine lt.