3 ms·
Maybe, but foldr (:) [] ~ id is an illuminating fact in its own right that is worth pausing to understand.
by danharaj 7y ago
Maybe, but foldr (:) [] ~ id is an illuminating fact in its own right that is worth pausing to understand.
- patrickthebold 7y agoTo spell things out a bit more: A list is fundamentally constructed from [] and :. In the sense that a list is either empty [] or a head : tail, where tail is another list. One might write 1:2:3:4:[]. foldr is deciding where to map [] and :. So for example: [] => 0 : => + Turns the above list into 1+2+3+4+0. With that understanding, foldr (:) [] becomes: [] => [] : => : which is fairly clearly the identity.
- toolslive 7y ago> foldr is deciding where to map [] and : This is a way better explanation than the cryptic: " it (foldr) takes the second argument and the last item of the list and applies the function, then it takes the penultimate item from the end and the result, and so on. See scanr for intermediate results. "
- kccqzy 7y agoIt absolutely is. It is the basis for the build/foldr stream fusion built into the standard library.