SML:从列表中删除条目 [英] SML: Remove the entry from the List
本文介绍了SML:从列表中删除条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何删除列表L
中的元素elem
?如果列表不包含elem,则函数应返回不变的列表.
How can I delete the element elem
in list L
? If the list does not contain elem, then the function should return the list unchanged.
例如:
L = [1, 3, 4, 0, 5, 7]
elem = 5
到目前为止,我具有以下功能:
So far I have the following function:
fun removeElem elem myList[] = myList
| removeElem (myList::tl) = if mem myList elem then
rm elem myList[]
else
removeElem elem tl
推荐答案
您可以解决这个问题,并询问如何仅保留那些不等于elem
的项目.这完全适合filter
:
You can turn the question around and ask how to keep only those items not equal to elem
. This fits cleanly with filter
:
fun removeElem elem myList = filter (fn x => x <> elem) myList
这篇关于SML:从列表中删除条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文