Is there any way to separate infinite and finite lists?

后端 未结 5 1625
猫巷女王i
猫巷女王i 2020-12-16 23:25

For example, I am writing some function for lists and I want to use length function

foo :: [a] -> Bool
foo xs = length xs == 100

How can

5条回答
  •  情书的邮戳
    2020-12-16 23:51

    On edit: I am primaily responding to the question in your title rather than the specifics of your particular example, (for which ErikR's answer is excellent).

    A great many functions (such as length itself) on lists only make sense for finite lists. If the function that you are writing only makes sense for finite lists, make that clear in the documentation (if it isn't obvious). There isn't any way to enforce the restriction since the Halting problem is unsolvable. There simply is no algorithm to determine ahead of time whether or not the comprehension

    takeWhile f [1..]
    

    (where f is a predicate on integers) produces a finite or an infinite list.

提交回复
热议问题