Harry
Harry

Reputation: 33

Haskell recursion odd

I have to define my own predicate function that recursively searches a list of numbers and returns True if it finds an odd number, False otherwise.

I have set X = [1..100] for test data that I need to show. However, I am a bit unsure about how to get recursion to search the list. I'm not looking for the definitive answer but an explanation on how recursion would go around searching in a list.

Upvotes: 0

Views: 489

Answers (1)

Aadit M Shah
Aadit M Shah

Reputation: 74204

What you want is the function any odd:

any :: (a -> Bool) -> [a] -> Bool

Odd :: Integral a => a -> Bool

any odd :: Integral a => [a] -> Bool

anyOdd :: Integral a => [a] -> Bool
anyOdd = any odd

If you want to define the anyOdd function yourself then you would have to use recursion:

anyOdd :: Integral a => [a] -> Bool
anyOdd []       = undefined
anyOdd (x:xs)
    | odd x     = undefined
    | otherwise = undefined

You will need to replace undefined with your actual logic.

Upvotes: 2

Related Questions