-
Notifications
You must be signed in to change notification settings - Fork 2
/
Problem73.hs
44 lines (37 loc) · 1.18 KB
/
Problem73.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Problem73 where
import MTree
import Text.Parsec hiding ((<|>))
import Control.Applicative
{-
I don't understand why the example is given in this way:
Tree> display lisp tree1
"a"
Tree> display lisp tree2
"(a b)"
why not just keep the same way as before:
a function that converts string to tree,
and another that converts the other way around
-}
tree2Lispy :: Tree Char -> String
tree2Lispy (Node v []) = [v]
tree2Lispy (Node v subs) = "("
++ unwords ([[v]] ++ map tree2Lispy subs)
++ ")"
lispy2Tree :: String -> Tree Char
lispy2Tree s = case parse parseTree "" s of
Left msg -> error (show msg)
Right t -> t
where
parseTree = (between (char '(')
(char ')')
(parseTree `sepBy1` space) >>= \(Node x []:xs) ->
return (Node x xs))
<|> Node <$> anyChar <*> pure []
main :: IO ()
main = do
print (tree2Lispy tree5)
print $ tree5 == (lispy2Tree . tree2Lispy) tree5
print $ tree2Lispy tree5 == ( tree2Lispy
. lispy2Tree
. tree2Lispy
) tree5