Skip to content

Instantly share code, notes, and snippets.

@bChiquet
Created October 9, 2017 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bChiquet/d1840879b760f18d37d967eb8778512c to your computer and use it in GitHub Desktop.
Save bChiquet/d1840879b760f18d37d967eb8778512c to your computer and use it in GitHub Desktop.
import Test.Hspec
main = hspec $ do
describe "tree fold" $
it "works!" $
visit (Branch
-- / \
(Node "h") (Branch
-- / \
(Branch -- \
-- / \ \
(Node "e") (Node "l"))-- \
(Branch
-- / \
(Node "l") (Node "o"))))
`shouldBe` "hello"
describe "tc tree fold" $
it "works too!" $
visit' (Branch
-- / \
(Node "h") (Branch
-- / \
(Branch -- \
-- / \ \
(Node "e") (Node "l"))-- \
(Branch
-- / \
(Node "l") (Node "o"))))
`shouldBe` "hello"
data Tree = Node String
| Branch Tree Tree
visit :: Tree -> String
visit (Node s) = s
visit (Branch a b) = visit a ++ visit b
visit' :: Tree -> String
visit' t = treeFold [t] ""
treeFold :: [Tree] -> String -> String
treeFold [] acc = acc
treeFold (t:ts) acc = case t of
Node s -> treeFold ts (acc ++ s)
Branch a b -> treeFold (a:b:ts) acc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment