[Haskell-beginners] Re: folds again -- myCycle

Will Ness will_n48 at yahoo.com
Wed Mar 18 11:14:03 EDT 2009

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. :)

> [let and where versions of myCycle] are compiled to exactly the same code. 

since there are no guards there with shared variables, I guess.

> 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?

Also, one can imagine this rewrite to be arrived at automagically by a compiler:

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. :)


More information about the Beginners mailing list