Vlad GURDIGA
2017-05-31 06:47:13 UTC
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
fold : (a -> b -> b) -> b -> Tree a -> b
Node v left right ->
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! ð
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:traversal.
fold : (a -> b -> b) -> b -> Tree a -> b
fold : (a -> b -> b) -> b -> Tree a -> b
fold f z tree =
case tree of
Empty ->
zcase tree of
Empty ->
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 thatz_ = 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_
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 elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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 elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.