I am writing a code in Haskell which take a list of 0's and 1's like [1,1,0,0,1,0,1] return a Pair(tuple) of the number occurrence of 0 and 1 in a list like (3,4).
here is my code
inc :: Int -> Int
inc x = (\x -> x + 1) x
count :: [Int] -> (Int,Int)
c = (0,0)
count x =
if null x
then c
else if head x == 0
then do
inc (fst c)
count (tail x)
else if head x == 1
then do
inc (snd c)
count (tail x)
i have also tried doing it in a guarded form
count :: [Int] -> (Int,Int)
c = (0,0)
count x
| null x = c
| head x == 0 = inc (fst c) >> count (tail x)
| head x == 1 = inc (snd c) >> count (tail x)
The main problem is that i am not sure how to implement 2 function in one then statement.
Aucun commentaire:
Enregistrer un commentaire