[Haskell-beginners] can I have a case statement with a pattern matching

Roelof Wobben r.wobben at home.nl
Wed May 13 16:52:38 UTC 2015


Hello,

I try to make make the assignment of CIS194 where I must multiply every  
second item from the right by 2

So I thought this could be working :

-- | Convert a digit to a reversed list for example 123 becomes [3,2,1]
revtoDigits :: Integer -> [Integer]
revtoDigits number
   | number <= 0 = []
   | number > 0 = number`mod` 10 : revtoDigits (number `div` 10)

-- | Convert a digit to a list for example 123 becomes [1,2,3]
toDigits :: Integer -> [Integer]
toDigits number
   | number <= 0 = []
   | number > 0 = toDigitsAcc [] number

toDigitsAcc:: [Integer] -> Integer -> [Integer]
toDigitsAcc acc number
   | number <= 0  = acc
   | number > 0 =  toDigitsAcc ((number `mod` 10) : acc) (number `div` 10)

doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther list  = case (length list) `mod` 2 of 0 -> 
doubleEveryOther [] = []
doubleEveryOther (x:y:xs) = x*2 : y : xs
                               (length list) `mod` 2 of 0 -> 
doubleEveryOther [] = []
doubleEveryOther (x:y:xs) = x : y*2 : xs


-- | The main entry point.
main = print $ doubleEveryOther [1,2,3]


but apperantly you cannot have pattern matching after a case statement 
because I get a error on the =  of a empty list

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com



More information about the Beginners mailing list