Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining infinite list in Haskell of same int, which way?

Tags:

list

haskell

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?

like image 523
vis Avatar asked Dec 01 '22 23:12

vis


2 Answers

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.

like image 179
jberryman Avatar answered Dec 04 '22 03:12

jberryman


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.

like image 26
leftaroundabout Avatar answered Dec 04 '22 03:12

leftaroundabout