[Haskell-beginners] can I have a case statement with a pattern matching
Alexey Shmalko
rasen.dubi at gmail.com
Thu May 14 16:46:48 UTC 2015
Then it could be:
doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther (x:y:xs) = x : 2 * y : doubleEveryOther xs
doubleEveryOther x = x
It could also be generalized to Num's ;)
On Thu, May 14, 2015 at 7:28 PM Jonathan Skårstedt <
jonathan.skarstedt at gmail.com> wrote:
> I'm nitpicking here, but it can be improved a bit
>
> doubleEveryOther :: [Integer] -> [Integer]
> doubleEveryOther [] = []
> doubleEveryOther [x] = [x]
> doubleEveryOther (x:y:xs) = x : 2 * y : doubleEveryOther (xs)
>
> 2015-05-14 8:48 GMT+02:00 Nishant <nishantgeek at gmail.com>:
>
>> 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
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
>
> --
> Jonathan Skårstedt
> Bergsgårdsgärdet 39
> Lgh 1108
> 424 32 Göteborg
> Mobil: 073 - 76 20 20 7
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150514/933561f2/attachment.html>
More information about the Beginners
mailing list