Jon H
Jon H

Reputation: 33

Count number of odd digits in Integer Haskell

I'm trying to make program which counts the number of odd digits in integer using Haskell. I have ran into problem with checking longer integers. My program looks like this at the moment:

oddDigits:: Integer -> Int
x = 0
oddDigits i
   | i `elem` [1,3,5,7,9] = x + 1
   | otherwise = x + 0

If my integer is for example 22334455 my program should return value 4, because there are 4 odd digits in that integer. How can I check all numbers in that integer? Currently it only checks first digit and returns 1 or 0. I'm still pretty new to haskell.

Upvotes: 0

Views: 607

Answers (3)

ƛƛƛ
ƛƛƛ

Reputation: 892

You can first convert the integer 22334455 to a list "22334455". Then find all the elements satisfying the requirement.

import Data.List(intersect)

oddDigits = length . (`intersect` "13579") . show

Upvotes: 5

Here's an efficient way to do that:

oddDigits :: Integer -> Int
oddDigits = go 0
  where
    go :: Int -> Integer -> Int
    go s 0 = s
    go s n = s `seq` go (s + fromInteger r `mod` 2) q
      where (q, r) = n `quotRem` 10

This is tail-recursive, doesn't accumulate thunks, and doesn't build unnecessary lists or other structures that will need to be garbage collected. It also handles negative numbers correctly.

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476567

In order to solve such problems, you typically split this up into smaller problems. A typical pipeline would be:

  1. split the number in a list of digits;
  2. filter the digits that are odd; and
  3. count the length of the resulting list.

You thus can here implement/use helper functions. For example we can generate a list of digits with:

digits' :: Integral i => i -> [i]
digits' 0 = []
digits' n = r : digits' q
    where (q, r) = quotRem n 10

Here the digits will be produced in reverse order, but since that does not influences the number of digits, that is not a problem. I leave the other helper functions as an exercise.

Upvotes: 4

Related Questions