Defining infinite list in Haskell:
[1,1..] => [1,1,1,..]
Or, the circular way:
lst=1:lst
Is the first defined the same as the second? If not, which one is the preferred way?
You probably want repeat
where the definition is equivalent to your second implementation.
The [1,1..]
notation in your first example is syntactic sugar for the enumFrom*
prelude functions. Use whichever you prefer.
repeat
/ 1:lst
are better, they don't require any extra calculation but [1,1..]
does:
[1,1..] = enumFromThen 1 1 = en 1
where en n = n : en (n + nΔ)
nΔ = 1-1 = 0
so it always needs to perform the extra 1+0
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With