Problem 73

Lisp-like tree representation.

a) Write a function of represent a multiway tree as a String, using the "Lispy" convention illustrated above.

b) Build an Tree from a "Lispy" string.

import Html exposing (text)


type Tree a = Node a (List (Tree a))


tree1 = Node 'a' []
tree2 = Node 'a' [Node 'b' []]
tree3 = Node 'a' [Node 'b' [Node 'c' []]]
tree4 = Node 'b' [Node 'd' [], Node 'e' []]
tree5 = 
  Node 'a' 
    [ Node 'f' 
      [ Node 'g' []]
      , Node 'c' []
      , Node 'b' 
        [ Node 'd' []
        , Node 'e' []
      ]
    ]
s5 = "(a(f(g)cb(de)))"

lispify : Tree a -> String
lispify t = 
    -- your implementation here
    ""


delispify : String -> Tree Char
delispify s = 
    -- your implementation here
    Node 'x' []

main =
    Html.text
        (if (test) then
            "Your implementation passed all tests."
         else
            "Your implementation failed at least one test."
        )


test : Bool
test =
    List.all ((==) True)
        [ tree4 == delispify "(b(d(d)))"
        , lispify tree4 == "(b(d(d)))"
        , tree5 == delispify s5
        , lispify tree5 == s5
        ]

Result

['g', 'f', 'c', 'd', 'e', 'b', 'a']

results matching ""

    No results matching ""