[Haskell-beginners] Traversing generic trees

Joel Williamson joel.s.williamson at gmail.com
Thu Jul 23 02:52:50 UTC 2015


Ali,

Nested lists with different nesting depths cannot be properly typed. You
could probably get around this with a heterogenous collection (
https://wiki.haskell.org/Heterogenous_collections), but the mechanisms
proposed there strike me as overkill for this problem. QuasiQuoters could
definitely do it, by defining a new grammar construct. That would be quite
advanced, and would teach you more about meta-programming than about trees,
algorithms or functional programming.

Using Imants' suggestion, you would write:

n [l 'a', n [l 'b', l 'c', n []], l 'd']

This is just as much boiler plate as you had in your initial definition of
tree, but it uses shorter identifiers.


Joel

On Wed, 22 Jul 2015 19:27 Ali Baharev <ali.baharev at gmail.com> wrote:

> Dear Imants,
>
> Thanks. Unfortunately, I am not sure I get it. It seems to me that it
> generates a homogeneous list of either nodes with single leaver or
> just leaves. Or how would the
>
> ['a', ['b', 'c', []], 'd']
>
> input look like, when using your proposed approach?
>
> Ali
>
> On Thu, Jul 23, 2015 at 1:10 AM, Imants Cekusins <imantc at gmail.com> wrote:
> > how about this:
> >
> >
> > data Tree a = L a | N [Tree a] deriving Show
> >
> >
> > l::a -> Tree a
> > l = L
> >
> > n::a -> Tree a
> > n x = N [L x]
> >
> > m::(a->Tree a) ->[a]-> Tree a
> > m f list = N $ f <$> list
> >
> >
> > run it like this:
> > m l [2,3]
> > m n [2,3]
> >
> > any good?
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150722/d4db69c9/attachment.html>


More information about the Beginners mailing list