Remove elements by index in haskell

后端 未结 8 850

I\'m new in haskell and I\'m looking for some standard functions to work with lists by indexes.

My exact problem is that i want to remove 3 elements after every 5. If i

8条回答
  •  迷失自我
    2021-02-07 23:53

    This is my solution. It's a lot like @barkmadley's answer, using only take and drop, but with less clutter in my opinion:

    takedrop :: Int -> Int -> [a] -> [a]
    takedrop _ _ [] = []
    takedrop n m l  = take n l ++ takedrop n m (drop (n + m) l)
    

    Not sure if it'll win any awards for speed or cleverness, but I think it's pretty clear and concise, and it certainly works:

    *Main> takedrop 5 3 [1..20]
    [1,2,3,4,5,9,10,11,12,13,17,18,19,20]
    *Main> 
    

提交回复
热议问题