[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 15 KB, 408x598, lambda.png [View same] [iqdb] [saucenao] [google]
2492221 No.2492221 [Reply] [Original]

Any Haskellfags willing to help a bro out ?
Is it possible in some meaningful way to fold over a binary tree defined as
data Tree a = Leaf a | Branch (Tree a) a (Tree a)
?
It's easy to fold over a tree with nullary Leaf, but the book I'm learning haskell from (yaht) uses unary leaf and wants me to define fold for it. HOW ?! Is it even possible ?

>> No.2492289
File: 300 KB, 1500x1013, 1292719302459.jpg [View same] [iqdb] [saucenao] [google]
2492289

Not even if I post my transhuman girls folder ?

>> No.2493306
File: 612 KB, 1024x1583, 1292732701409.jpg [View same] [iqdb] [saucenao] [google]
2493306

Last try

>> No.2493320

>>2492289

>Implying everyone of /sci/ doesn't have every picture in existence of Kimiko.

>> No.2493327

>>2492221
Ask in /prog/, we have plenty of haskellers there. If not, ask in #haskell on freenode.
I can see how to do it in ML and Common Lisp, but I don't know Haskell well enough to answer your question.

>> No.2493349

>>2493327
I know a little lisp, so it would be helpful too.

The only thing I don't know is the edge case... what to do with leaf

>> No.2493395

>>2493349
In the leaf case, you just return the value.
In the main case, you call the function on either the left branch and the folded right branch (foldr) or the reverse case (foldl) or on both.

>> No.2493472

>>2493395
Well, the next exercise in the book wants me to use this fold to construct a list from the tree, so in order for this to be possible, I'd have to return [x] (a list containing just one element) which is not good for a general fold...

Also, I'd say that (fold bla bla (Leaf x)) shouldn't behave as identity on x, because fold on one-element lists doesn't.

>> No.2493477

>>2493472
A list from a tree? How about /flatten/ and a map?

>> No.2493508

>>2493477
Yeah it can be done easily. It's easy even with explicit recursion... fuck it. sage, I'll skip this one.