Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple yes/no haskell list question

Tags:

list

haskell

So I'm reading http://learnyouahaskell.com/starting-out as it explains lists, and using ghci on Vista 64. It says that [2,4..20] steps by 2 from 4 to 20. This works. It says [20,19..1] goes from 20 to 1, but doesn't explain. I take it that the first number is NOT the step, the step is the difference between the 1st and 2nd number. This is confirmed by [4,4..20] which hangs (no error message, must kill console). This is unlike operators like !! and take which check the index's range and give an error message.

My question is: is this a bug on Vista port or is that the way it's supposed to be?

like image 734
paul Avatar asked Nov 29 '22 19:11

paul


1 Answers

[x,y..z] does indeed step from x to z by step y-x. When y-x is 0 this leads to an infinite list. This is intended behavior.

Note that if you use the list in an expression like take 20 [2,2..20], ghci won't try to print the whole list (which is impossible with infinite lists of course) and it won't "hang".

like image 58
sepp2k Avatar answered Dec 05 '22 05:12

sepp2k