How to implement delete with foldr in Haskell

后端 未结 3 712
無奈伤痛
無奈伤痛 2021-02-19 04:13

I\'ve been studying folds for the past few days. I can implement simple functions with them, like length, concat and filter. What I\'m stu

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-19 04:52

    here is a simple delete, implemented with foldr:

    delete :: (Eq a) => a -> [a] -> [a]
    delete a xs = foldr (\x xs -> if x == a then (xs) else (x:xs)) [] xs
    

提交回复
热议问题