【发布时间】:2016-01-02 02:17:49
【问题描述】:
我正在编写一个使用一个列表作为事件的函数。然后该函数获取此列表并从另一个列表中删除所有出现的它。
例如:
[1,2,3] 应该从 [3,2,1,2,3,1,2,3] 中删除,从而得到 [3,2]
[1,2,3] 不应该被删除,因为我会得到一个空列表 []
到目前为止,我已经将其删除所有事件之一,但不会删除任何其他事件。
这是我的功能:
fun deleteAll l1 [] = []
| deleteAll l1 (hd::tl) =
if starts l1 (hd::tl)
then (deleteAll l1 tl; delete l1 (hd::tl))
else [hd]@deleteAll l1 tl;
以下是其中使用的其他函数:
fun starts [] l2 = true
| starts l [] = false
| starts (h1::t1) (h2::t2) = (h1=h2) andalso (starts t1 t2);
fun delete l1 [] = []
| delete l1 (hd::tl) =
if starts l1 (hd::tl)
then List.drop(hd::tl, length l1)
else [hd]@delete l1 tl;
【问题讨论】:
-
请记住,您不是在使用可变数据进行编程;
deleteAll l1 tl; delete l1 (hd::tl)等价于delete l1 (hd::tl)。 -
@molbdnilo 那么我该如何克服这个问题来解决我的问题呢?