[Haskell-beginners] recursive 'let' ?
patrick.john.wheeler at gmail.com
Mon Apr 14 10:09:20 UTC 2014
Top level values have the attributes that you describe, recursion, mutual
recursion are possible.
So by using `let` and `where`, with out knowing any additional rules or
information, we can define values and functions which can be locally
scoped. Reducing the number of top level values by using locally scoped
vales often makes it easier to think about a problem.
You concern seems to be focused on the mutual recursion aspect however. Why
it is it useful in general? It allows for problems to be broken down in to
sub problems and solved separately.
With out mutually recursion or something similar if you had a problem that
need to recurse in two different ways, each which some times depended on
the other you would have to write it all in one large function.
An example where this is used in serious cod would be pipes and conduit.
Both handle stream processing of data.
Mutual recursion between (>>~) and (+>>)
Look at the goRight and goLeft functions that are locally scoped with
`where` in either the `pipe` or `pipeL` functions.
So can help as a tool to break some problems in to subproblems and it is
used in serious code in Haskell
On Fri, Apr 11, 2014 at 10:28 AM, Magnus Therning <magnus at therning.org>wrote:
> On Thu, Apr 10, 2014 at 07:12:25PM -0500, John M. Dlugosz wrote:
> > I understand that the definitions introduced by 'let' can be
> > recursive, even mutually recursive among several names. Why would
> > you want to do that? I saw contrived examples, and wonder why the
> > authors never show a realistic example.
> > let b = f a c
> > c = f a b
> > in ...
> > I see that makes sense in light of lazy evaluation: b is really an
> > alias for a (recursive) function, not a value that needs to find
> > fixed points.
> > Is this used for common idioms and problem-solving approaches in
> > Haskell?
> I can't really point to any idiomatic use, or problem-solving
> approaches, but it is a terribly useful feature since one of the
> effects is that the order of introducing functions/values isn't
> significant. So you are free to write and structure your code in the
> manner that makes most sense to you.
> Having just ventured back into OCaml and dipping my toes in F# I
> immediately ran into errors caused by their non-recursive `let`, and
> the requirement to introduce values/types before use.
> Magnus Therning OpenPGP: 0xAB4DFBA4
> email: magnus at therning.org jabber: magnus at therning.org
> twitter: magthe http://therning.org/magnus
> The results point out the fragility of programmer expertise: advanced
> programmers have strong expectations about what programs should look like,
> and when those expectations are violated--in seemingly innocuous
> ways--their performance drops drastically.
> -- Elliot Soloway and Kate Ehrlich
> Beginners mailing list
> Beginners at haskell.org
Patrick.John.Wheeler at gmail.com
Patrick.J.Wheeler at rice.edu
Patrick.Wheeler at colorado.edu
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners