Hi! 👋

I’m trying to get through the exercises in the Binary Tree example here: 
http://elm-lang.org/examples/binary-tree.

I’ve got to the 4-th and it seems to me like it’s impossible to solve (with 
the understanding I have now).  🤔

So, here it goes:

(4) Write a general fold function that acts on trees. The fold
>     function does not need to guarantee a particular order of
>     traversal.
>
>        fold : (a -> b -> b) -> b -> Tree a -> b


…and here is my attempt to solve it:

fold : (a -> b -> b) -> b -> Tree a -> b
> fold f z tree =
>     case tree of
>       Empty -> 

        z
>       

      Node v left right ->
>         let
>           z_ = f v z
>           l_ = fold f z left
>           r_ = fold f z right
>         in
>           {- TODO: figure out how to combine the 3 values of type b -}
>           f v z_


The issue I’m stuck with in the last case —​ Node v left right -> — is that 
I now got 3 values of type b which I can’t fold into the final result: f has 
the type of (a -> b -> b), so if the only value of type a here is z, the 
most I can do is one folding operation, but I get another b as a result. 😶

My question is: How can I fold the 3 values? Or is this approach workable 
at all? What am I missing? 🤔

Cheers! 👋

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to