I taught myself Haskell for a month or so, and today I read the solution to the 16th problem and came up with a question.
Here is the link: http://www.haskell.org/haskellwiki/99_questions/Solutions/16
Basically, this question asks for a function that removes each element of N from the list. For instance,
*Main> dropEvery "abcdefghik" 3
"abdeghk"
First solution in link
dropEvery :: [a] -> Int -> [a]
dropEvery [] _ = []
dropEvery (x:xs) n = dropEvery' (x:xs) n 1
where
dropEvery' (x:xs) n i = (if (n `divides` i) then [] else [x])++ (dropEvery' xs n (i+1))
dropEvery' [] _ _ = []
divides x y = y `mod` x == 0
My question is: why does dropEvery determine the case of empty lists, while dropEvery 'can take care of an empty list? I think itโs dropEvery [] _ = []possible to simply eliminate and modify a few other suggestions, as the following should work exactly the same as above and look shorter.
dropEvery :: [a] -> Int -> [a]
dropEvery xs n = dropEvery' xs n 1
where
dropEvery' (x:xs) n i = (if (n `divides` i) then [] else [x])++ (dropEvery' xs n (i+1))
dropEvery' [] _ _ = []
divides x y = y `mod` x == 0
Can someone help me figure this out?
Tengu source
share