Problem 70b Solutions

import Html import List import Maybe import String

stringToTree : String -> MTree Char
stringToTree s = 
    let 
        (x, xs) = Maybe.withDefault ('?', "") <| String.uncons s
    in
        MNode x (Tuple.first (sToTrees xs))


sToTrees : String -> ((List (MTree Char)), String)
sToTrees s = 
    let 
        (x, xs) = Maybe.withDefault ('?', "") <| String.uncons s
    in
        case x of 
            '^' ->
                ([], xs)

            _ ->
                let 
                    (list1, rest1) = sToTrees xs
                    (list2, rest2) = sToTrees rest1
                in 
                    ([(MNode x list1)] ++ list2, rest2)


treeToString : MTree Char -> String
treeToString tree =
    t2s tree ++ "^"

t2s : MTree Char -> String
t2s tree =
    case tree of 
        (MNode v []) ->
            String.fromChar v 

        (MNode v list) ->
            String.cons v 
                <| (String.concat 
                     (List.intersperse 
                       "^" (List.map t2s list))) 
                   ++  "^"

Back to problem

results matching ""

    No results matching ""