The "Guards vs. If-Then-Else" helped a little but I still want to know if this can work somehow. I need to take a list and return every other element of the list. For even length lists I got it just if length (xs) mod 2 == 1 to start is there an issue so I want to break up the initial length of list case like this:
everyOther:: [Int] -> [Int]
everyOther [] = []
everyOther (x:xs) = case length (xs) 'mod' 2 of 0 ->
if (length (xs) `mod` 2 == 0)
then x:everyOther (take 1 xs)
else x:everyOther (drop 1 xs)
1 -> if (length (xs) `mod` 2 == 1)
then x:everyOther (take 1 xs)
else x:everyOther (drop 1 xs)
It's telling me there is a possible "spacing error" at the "if" but is this functionally correct. Can I do this?
Aucun commentaire:
Enregistrer un commentaire