我正在尝试编写一个删除列表中第二次出现的元素的函数。 目前,我编写了一个删除第一个元素的函数:
removeFirst _ [] = []
removeFirst a (x:xs) | a == x = xs
| otherwise = x : removeFirst a xs
作为起点。但是,我不确定这个功能是否可以通过列表理解来完成。有没有办法使用map实现这个?
编辑:现在我添加了一个调用第一个
的removeSecond函数 deleteSecond :: Eq a => a -> [a] -> [a]
deleteSecond _ [] = []
deleteSecond a (x:xs) | x==a = removeFirst a xs
| otherwise = x:removeSecond a xs
但是现在返回的列表会删除元素的第一次和第二次出现。
答案 0 :(得分:5)
好吧,假设你有removeFirst
- 如何搜索第一次出现,然后在剩余列表中使用removeFirst
?
removeSecond :: Eq a => a -> [a] -> [a]
removeSecond _ [] = []
removeSecond a (x:xs) | x==a = x:removeFirst a xs
| otherwise = x:removeSecond a xs
答案 1 :(得分:3)
您也可以将其作为折叠实现。
removeNth :: Eq a => Int -> a -> [a] -> [a]
removeNth n a = concatMap snd . scanl go (0,[])
where go (m,_) b | a /= b = (m, [b])
| n /= m = (m+1, [b])
| otherwise = (m+1, [])
并在行动中:
λ removeNth 0 1 [1,2,3,1]
[2,3,1]
λ removeNth 1 1 [1,2,3,1]
[1,2,3]
我使用了scanl
而非foldl
或foldr
,因此它可以从左到右传递状态并处理无限列表:
λ take 11 . removeNth 3 'a' $ cycle "abc"
"abcabcabcbc"
答案 2 :(得分:0)
以下是使用List
提供的函数的本能实现:
import List (elemIndices);
removeSecond x xs = case elemIndices x xs of
(_:i:_) -> (take i xs) ++ (drop (i+1) xs)
_ -> xs
removeNth n x xs = let indies = elemIndices x xs
in if length indies < n
then xs
else let idx = indies !! (n-1)
in (take idx xs) ++ (drop (idx+1) xs)
注意:这个不能处理无限列表,并且它的性能可能不适合非常大的列表。