Created
October 9, 2017 13:09
-
-
Save bChiquet/d1840879b760f18d37d967eb8778512c to your computer and use it in GitHub Desktop.
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
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