我想我希望它是'列表*'類型列表 - >'列表。爲什麼我的功能鍵入'列表*'列表 - >'B列表?
相交應返回兩個列表 樣本的輸入和輸出的交集:
- 交點([1],[1]);
- [1]
- 交點([1,2,3],[1,2]);
- [1,2]
- 交點([[2,3],[1,2],[2,3]],[[1],[2,3]]);
- [2,3]
我的功能:
fun intersection (l1, l2) = let
fun intersection_acc (acc, [], h::t) = []
| intersection_acc (acc, h::t, []) = []
| intersection_acc (acc, h::t, h2::t2) = if in_list (h, l2)
then intersection_acc (h::acc, t, l2)
else intersection_acc (acc, t, l2)
in intersection_acc ([], l1, l2)
end
我不認爲in_list的問題,但是這看起來是這樣的:
fun in_list (x, []) = false
| in_list (x, y::r) = if x = y
then true
else in_list (x, r);
你爲什麼標記此作爲matlab?這顯然是一些ML變體,而不是matlab。我在猜SML? – sepp2k
對不起,我沒有意識到有什麼不同。我認爲這是SMLNJ。 – Nate
@Nate:ML與matlab無關。 ML是一種功能語言,MatLab勢在必行。 –