## DEV Community is a community of 755,328 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Define a function dropOdds :: Int -> Int with the following behaviour. For any positive number m, dropOdds m is got by dropping all the odd digits in m. (If all the digits in the number are odd, the answer should be 0.)
Test cases:
dropOdds 0 = 0
dropOdds 8 = 8
dropOdds 1357 = 0

## Discussion (8) Phantz

• How can you turn a number into a list of "digits".
• How can you check if a "digit" is odd?
• How do you "drop" certain elements of a list that match a criteria? (hint: filter)

You'll notice the quotes I put around the word "digits". I generally prefer using `Char` for "digits". But one may just as well use `Int`. Whichever you use, your answers will depend on it. Benjioe

With Recursion :

``````dropOdds :: Int -> Int
dropOdds x
| x >= 10 = 10 * dropOdds restDigits + filterDrops firstDigit
| otherwise = filterDrops x
where
firstDigit = x `mod` 10
restDigits = x `div` 10

filterDrops x
| x `mod` 2 == 0 = x -- x is even => we keep it
| otherwise = 0 -- x is drop => we

main =  do
print(dropOdds 0)
print(dropOdds 8)
print(dropOdds 10)
print(dropOdds 1357)
``````