Talk:Haskell/Other data structures

What is an unfold?
There is a section on unfolds that says nothing other than the implementation for one in Haskell. We need a better explanation for that. I don't know what it is. --Digichoron (discuss • contribs) 02:55, 6 April 2011 (UTC)


 * I feel that that unfold does not belong here at all... --Duplode (discuss • contribs) 06:04, 4 April 2012 (UTC)


 * Section removed. Permalink to last revision containing it.--Duplode (discuss • contribs) 01:29, 12 April 2012 (UTC)

This chapter is challenging
Compared with chapters up to this point, this is difficult. I have a hard time making sense of everything. Note that I changed the generic "z" in the foldr definition back to the "acc" that was used when folds were first taught. There's a strangeness in making the analogy between the accumulator value and the empty-list in that they are used in the same place in some sense, but the accumulator is not always nil or zero. Somehow, this needs to be clarified in a way that makes the process clearer. Backfromquadrangle (discuss • contribs) 20:47, 30 April 2014 (UTC)


 * This is a quaint chapter, and I never felt sure about what should be done with it. It is certainly a good thing to show how a recursive data structure and functions on it can be defined, and  helps setting the stage for introducing   two chapters ahead. The generalised examples are definitely challenging for newbies. That is a little jarring, but also makes for a good workout/self-test of fundamental code reading/writing skills. They are also rich in insights, but with the current presentation they are likely to go over the head of beginners. The "general fold" presentation is strange, in that it boils down to introducing catamorphisms (i.e. the generalised recursion scheme of folds) without actually calling them by their name. Catamorphisms aren't something you'd expect to see being discussed in a text aimed at complete newcomers either...


 * A final issue is that I believe it is hard to justify going through the entire Beginner's Track without even mentioning the core data structure libraries. I plan to rectify that with the currently WIP Data Structures Primer chapter. Though it will probably should go to the "In Practice" track, once it comes into being we could provide a pointer to it here. Another nice thing to do would be adding a chapter on  and  ; however, it would be difficult to place them here without reshuffling half of the book (  uses  ;   will probably make more sense once readers know about  ).--Duplode (discuss • contribs) 10:51, 1 May 2014 (UTC)

Clarifying which fold is described
In the "Fold" section, the description "When the list runs out, we are back at the empty list, so foldr returns whatever is then the accumulator value from the last fold." is for a, rather than a. Knotaken (discuss • contribs) 01:59, 24 May 2015 (UTC)

After considering this further, I think I can make my comment more useful by providing a basis for dicussion, if needed. Here's one possible edit that might address my comment:

"With a non-empty list, each item in the list will be combined with the accumulated result of folding the rest of the list . Since only the first argument to the function  is known, the function can't produce a new accumulated value until the rest of the list is folded.  The fold of that item into the result can be considered pending.  Continuing with the example using (+), each item is added to the result of adding the items in the rest of the list."

"Each item in the list is used in a pending fold. When the list runs out and we have an empty list, the accumulator is given the initial value supplied in the  call.  The accumulator can then be used to complete the fold, or applied to, with the most recent list item seen, which would have been the last item in the list.  Each completed fold provides a new accumulator to complete the fold before it.  That process continues back to the beginning of the list, where   returns the final accumulated value." Knotaken (discuss • contribs) 03:49, 24 May 2015 (UTC)


 * Thanks for drawing my attention to that -- I missed your comment back when you originally posted it. I think that passage of the text wasn't wrong, but just confusingly worded. That being so, I opted to simplify the explanation, in an attempt to remove the potentially confusing parts. As for your suggested edit, laziness means "the function can't produce a new accumulated value until the rest of the list is folded" is, from a certain point of view, not always the case when it comes to . Consider  : you can implement it with   and still successfully handle infinite lists (you just have to use something like   to discard the part of the result you aren't interested in). It is true that , in a sense, proceeds in a bottom-up manner (or, as you put it, "continues back to the beginning of the list"). Getting that idea across in a precise way, though, can be tricky. --Duplode (discuss • contribs) 00:02, 6 May 2019 (UTC)

How would weirdMap handle the Fifth constructor?
We get to see how to implement a fold for a complicated recursive datatype, but not how the map would be implemented for the a complicated recursive datatype. After the Weird type is changed, weirdMap can no longer handle the Fifth constructor.

I can try to imagine what the answer might be, but it'd be nice to be able to check the right answer. Did I miss something? Mttpgn (discuss • contribs) 18:22, 17 May 2019 (UTC)


 * Well-spotted! That case should look like this (spoiler warning :)):




 * If I ever get to add exercises to the chapter, I will include this one. (Though, if I actually get around changing it, it is just as likely that I end up trying to find a less contrived example to use here...)


 * Another thing the book perhaps should have mentioned, and that you might want to try: just like  can be implemented using , we can implement   using  . (Note that doing so with the five constructor version of   gets rather nasty, as that   field makes things awkward.)


 * Cheers, Duplode (discuss • contribs) 22:47, 17 May 2019 (UTC)