[Haskell-cafe] Re: Shouldn't this loop indefinitely => take (last [0..]) [0..]

John Meacham john at repetae.net
Sun Apr 6 20:59:29 EDT 2008


On Sun, Apr 06, 2008 at 11:30:20AM -0300, Felipe Lessa wrote:
> On Sun, Apr 6, 2008 at 11:12 AM, John Meacham <john at repetae.net> wrote:
> >  I implemented this efficient lazy natural class once upon a time. it
> >  even has things like lazy multiplication:
> [...]
> >  instance Num Nat where
> >     Zero + y = y
> >     Sum x n1 + y = Sum x (y + n1)
> >     --x + Zero = x
> >     --Sum x n1 + Sum y n2 = Sum (x + y) (n1 + n2)
> [...]
> 
> May I ask you why the last line above was commented out?

Notice it flips the order of the arguments with each iteration. This
allows it to avoid space leaks in some cases, for instance if you have
infinity + (space wasting thunk), the space wasting thunk will never be
deallocated even though it isn't used. It also means that the strictness
properties are more symmetric than they would be otherwise as people
expect of (+).

        John

-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Haskell-Cafe mailing list