[Haskell-cafe] flip fix and iterate
jules at jellybean.co.uk
Tue Mar 20 07:38:24 EDT 2007
Matthew Brecknell wrote:
> There is the question of whether it's preferable to use the "let" form
> or the "fix" form for embedding a recursive function in the middle of a
> do-block. I don't know if there's any consensus on this question, but it
> seems to me to be about whether one prefers to read a function top-down
> or bottom-up. I think I'm about 80/20 top-down/bottom-up. When I read a
> "let", I know (due to laziness) that it doesn't have any effect until
> the bindings are used, so I usually find myself scanning forward to find
> those uses. When I read "fix \f -> ...", I see exactly how the
> (anonymous) function is used, just before I get to its definition. So
> fix helps me to see things in a mostly top-down fashion.
If you're merely talking about top-down or bottom-up then there is also
'where' rather than 'let'.
So the question becomes:
1. recursive definition first (let)
2. recursive definition last (where)
3. recursion inline (explicit use of fix)
1+2 have the virtue of separating the local recursive function from the
'main' function. On the other hand, 3 has the virtue of keeping them
together :) Which you prefer is quite subjective, and context dependent.
They potentially have slightly different scoping implications, too.
A lot of discussion about haskell code revolves around whether or not a
given construction is 'clear'; this has something to do with haskell's
almost unparallelled ability to abstract, I suppose. Ultimately,
something is clear once you are used to the abstraction, but obfuscated
if you're not, so it becomes rather subjective.
More information about the Haskell-Cafe