[Haskell-cafe] Odd list comprehension behaviour

Krisztian Pinter pin.terminator at gmail.com
Thu Mar 17 02:58:06 UTC 2016


Hello,

I noticed some odd behaviour with list comprehensions.

[1..1] == [1]
BUT
[1,1..1] == [1..]

I noticed this while writing a Clean program, but it seems Haskell
inherited this as well.
In the case of integer lists with step size >= 0 the up_list function[1] is
used:

up_list :: Integer -> Integer -> Integer -> [Integer]
up_list x0 delta lim = go (x0 :: Integer)
                    where
                        go x | x > lim   = []
                             | otherwise = x : go (x+delta)

In the case of [1,1..1] x0 == lim, so go will recurse infinitely, producing
an infinite list.

I think the reasonable behaviour would be [1,1..1] == [1]. Is there a
reason it doesn't work like this?

[1]
http://hackage.haskell.org/package/base-4.8.2.0/docs/src/GHC.Enum.html#up_list

Thanks,
Krisztián
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160317/651455bf/attachment.html>


More information about the Haskell-Cafe mailing list