[Haskell-beginners] Re: folds again -- myCycle
daniel.is.fischer at web.de
Wed Mar 18 12:03:06 EDT 2009
Am Mittwoch, 18. März 2009 16:14 schrieb Will Ness:
> Daniel Fischer <daniel.is.fischer <at> web.de> writes:
> > Am Mittwoch, 18. März 2009 12:19 schrieb Will Ness:
> > > Bas van Dijk <v.dijk.bas <at> gmail.com> writes:
> > > > On Sun, Mar 15, 2009 at 7:35 PM, Will Ness <will_n48 <at> yahoo.com>
> > wrote:
> > > > > myCycle xs = ys where ys = foldr (:) ys xs
> > Of course a matter of personal preference, but I tend to prefer where
> > clauses, too, in general. However, my preferred layout is
> > some code
> > where
> > local declarations
> > I deeply loathe not having the where on a separate line :-/
> Will try not to offend in the future. :)
Very kind of you. But stick to your own style, I can live with loathing the
layout of some code :-)
> > AFAIK,
> > [let and where versions of myCycle] are compiled to exactly the same
> > code.
> since there are no guards there with shared variables, I guess.
No, that doesn't matter. GHC-core has no where, only let, so all where clauses
must be rewritten to use let.
> > What matters is whether you give a name to the result to get it shared or
> > not.
> I was hoping GHC is smarter than that in finding shared expressions. Is it
> what's called deforestation?
Sorry, don't know about that, but I think not,
common-subexpression-elimination sounds more like it. But I'm guessing here.
> Also, one can imagine this rewrite to be arrived at automagically by a
> sum $ take m $ cycle [1..k]
> | n > 0 = x*n+y
> (n,r) = quotRem m k
> x = sum [1..k]
> y = sum [1..r]
> Any human is certainly capable of seen this almost immediately, presented
> with the big k's and huge m's. It's automagical. :)
But it's too much of a special case to have a special rule for it in the
Humans are much less principled and can thus spot a greater variety of
patterns (but they are also better in overlooking such patterns).
More information about the Beginners