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

Nishant nishantgeek at gmail.com
Thu May 14 06:48:37 UTC 2015


This will double very second digit in list.  To do from right, just reverse
the list first and then apply doubleEverySecond and then reverse back.



doubleEveryOther :: [Integer] -> [Integer]

doubleEveryOther [] = []
doubleEveryOther (x : []) = [x]
doubleEveryOther (x : y : []) = x : (2 * y) : []
doubleEveryOther (x:y:xs) = x : (2 * y) : doubleEveryOther (xs)


On Wed, May 13, 2015 at 10:22 PM, Roelof Wobben <r.wobben at home.nl> wrote:

> 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
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Nishant
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150514/3909a71c/attachment-0001.html>


More information about the Beginners mailing list