[Haskell-cafe] Re: memory issues

Martin Huschenbett huschi at gmx.org
Sun Mar 1 12:30:48 EST 2009


ChrisK schrieb:
> Bulat is right about making Block's fields strict.
> 
>>
>> -- | Get the offsets between entries in a list
>> getSizes :: [Integer]  -> [Integer]
>> getSizes (x:y:[]) = [y - x]
>> getSizes (x:y:ys) = (y - x):(getSizes (y:ys))
> 
> You should change the first part to add maxSize:
> 
>  > getSizes :: [Integer]  -> [Integer]
>  > getSizes (x:y:[]) = [y - x,maxSize]
>  > getSizes (x:y:ys) = (y - x):(getSizes (y:ys))
> 
> This avoids the ugly use of (++) below.  Note that appending to a singly 
> linked list is a bad "code smell":

But Chris' version changed semantics. It should be

> getSizes :: [Integer] -> [Integer]
> getSizes (x:y:[]) = [y-x,maxSize-y]
> getSizes (x:y:ys) = (y-x):getSizes (y:ys)

instead. But

> getSizes :: [Integer] -> [Integer]
> getSizes [x] = [maxSize-x]
> getSizes (x:y:ys) = (y-x):getSizes (y:ys)

is even better as it doesn't repeat the y-x term.

Regards,

Martin.


More information about the Haskell-Cafe mailing list