Saturday, 30 April 2016

Haskell: cycle: Infinite list


cycle takes a list and cycles it into an infinite list.
Prelude> take 12 (cycle [2, 3, 5, 7, 11])
[2,3,5,7,11,2,3,5,7,11,2,3]
Prelude> 
Prelude> take 20 (cycle [2, 3, 5, 7, 11])
[2,3,5,7,11,2,3,5,7,11,2,3,5,7,11,2,3,5,7,11]
Prelude> 
Prelude> take 2 (cycle [2, 3, 5, 7, 11])
[2,3]


Since Lazy evaluation is one fo the fundamental feature of Haskell, it won't try to evaluate the infinite list immediately because it would never finish. It execute as per your requirement.

Previous                                                 Next                                                 Home

No comments:

Post a Comment