[Haskell-cafe] Odd list comprehension behaviour

Richard A. O'Keefe ok at cs.otago.ac.nz
Thu Mar 17 03:44:12 UTC 2016

On 17/03/16 3:58 pm, Krisztian Pinter wrote:

> I think the reasonable behaviour would be [1,1..1] == [1]. Is there a 
> reason it doesn't work like this?
That's arguably *a* reasonable behaviour, but I'm not sure it's *the*
reasonable behaviour.  Another reasonable behaviour would be a
run-time error for [X,X..Y] whatever Y is.  And to my mind, what
Haskell actually does, while not as programmer-friendly as a
run-time error would be, is reasonable because consistent.

In R,
 > seq(from=1, to=2, by=0)
Error in seq.default(from = 1, to = 2, by = 0) :
   invalid (to - from)/by in seq(.)
 > seq(from=1, to=1, by=0)
[1] 1

I find this behaviour in R inconsistent and confusing.
At least in Haskell [1,1..N] gives the same answer for
any N >= 1.

More information about the Haskell-Cafe mailing list