更新时间:2022-10-15 09:19:39
You know that the list is monotonically increasing, but Haskell does not. Use takeWhile
, instead of a list comprehension, so that list1
can stop being evaluated once you find a value greater than 4.
> list1 = [1..]
> list2 = takeWhile (<= 4) list1
> print list2
[1,2,3,4]