Writing Haskell code that drop every nth element from a list

Hi,

first please format/mark your code - thanks.

Do your problem start here:

dropevery :: [a] -> Int -> [a]
dropevery [] = []

you should already see an error here - you did not bind/name/declare (whatever you might call it) the argument for the Int - so the typechecker sees [] (some empty list) but your signature says this should be a function Int -> [a].

Try

dropevery :: [a] -> Int -> [a]
dropevery [] _ = []

Then I think you’ll find this easier without trying to write this with remove but in this form:

dropevery :: [a] -> Int -> [a]
dropevery xs n = go 0 xs
    where go _ [] = []
          go i (y:ys) | i == n = ?
                      | otherwise = ?
    

The idea is to to use a helper that moves along the list counting up i and of course once you’ll reach n you want to drop the current element (and continue with i=0 again).
Think about how you can express this with recursive calls to go where I put the ?


Depending on how far you are into your course (I guess this is some form of exercise / homework) you might be able to use/reuse some functions.
For example if you’ve already seen zip, filter and map there is a nice solution where you pair up the elements in the list with their indizes (it’s a default trick you’ll probably see in the lessons: zip [1..] xs) - then you can use this index X element paired list to filter out all elements where index mod n == 0 and then use map to drop the index again - this could turn out into one of those nice Haskell one-liners:

dropevery :: [a] -> Int -> [a]
dropevery xs n = map (..?..) $ filter (..?..) $ zip [1..] xs
3 Likes