Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list of n zeros in Haskell

This is probably very easy, but I cannot figure out how to do the equivalent of Python's

[0]*n

in Haskell, in order to get a list with n zeros.

[0]*n

doesn't work. Am I obliged to do something like: [0 | x <-[1..5]] ?

like image 659
Pigna Avatar asked Jan 06 '23 08:01

Pigna


1 Answers

You can do this:

λ> take 5 (repeat 0)
[0,0,0,0,0]

Or as @obadz points out, this is even more concise:

λ> replicate 5 0
[0,0,0,0,0]

I personally don't like the python syntax. * means multiplication but it does something else in your case. But that's just my opinion :).

like image 197
Sibi Avatar answered Jan 15 '23 06:01

Sibi