Remove elements by index in haskell

后端 未结 8 847

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> 
    
    0 讨论(0)
  • 2021-02-07 23:55
    myRemove = map snd . filter fst . zip (cycle $ (replicate 5 True) ++ (replicate 3 False))
    
    0 讨论(0)
提交回复
热议问题