'In Haskell, find the smallest element satisfying a condition [closed]

Suppose P is a condition involving natural numbers. How can I find in Haskell the smallest natural number n such that P(n) is true ?



Solution 1:[1]

It depends on P. The naive approach is just scanning from 0, but it might be too slow depending on P.

p :: Int -> Bool
p n = ... --- Your condition

v :: Int
v = head $ filter p [0..]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 snak