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.
For instance:
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
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