Sml 将列表1乘以列表2,得到;警告:匹配非穷举“;
据我所知Sml 将列表1乘以列表2,得到;警告:匹配非穷举“;,sml,smlnj,Sml,Smlnj,据我所知 fun addX (X, []) = [] | addX (X, y::ys) = (X + y :: addX(X, ys)); 工作非常好,但当我尝试用此方法将list1乘以list2时,它会给我“警告:匹配非穷举”,下面是我的代码: fun multList ([], []) = [] | multList (x::xs, y::ys) = (x * y :: multList(xs, ys)); 我做错了哪一部分?感谢您的帮助 由于x::xs和y::ys与“非空列表
fun addX (X, []) = []
| addX (X, y::ys) = (X + y :: addX(X, ys));
工作非常好,但当我尝试用此方法将list1乘以list2时,它会给我“警告:匹配非穷举”,下面是我的代码:
fun multList ([], []) = []
| multList (x::xs, y::ys) = (x * y :: multList(xs, ys));
我做错了哪一部分?感谢您的帮助 由于
x::xs
和y::ys
与“非空列表”匹配,因此您当前的代码仅与以下模式匹配:
。。。两个列表都是空的([],[])
。。两个列表都不是空的(x::xs,y::ys)
fun
multiList ([],[]) = []
| multiList (X,[]) = X
| multiList ([],X) = X
| multiList (x::xs, y::ys) = (x*y ::multiList(xs,ys));
当列表为空时,此代码返回非空的
Edit:正如@ruakh在评论中所说的,下面的代码更好,因为它似乎是multiList
的自然行为,但我将把上面的代码留作解释
fun
multiList ([],[]) = []
| multiList (x::xs, y::ys) = (x*y ::multiList(xs,ys))
| multiList _ = raise Fail "lists of non equal length";
请注意,
是通配符,因此当([],[])
和(x::xs,y::ys)
都不匹配时,它匹配任何内容。由于x::xs
和y::ys
与“非空列表”匹配,您当前的代码只匹配以下模式:
。。。两个列表都是空的([],[])
。。两个列表都不是空的(x::xs,y::ys)
fun
multiList ([],[]) = []
| multiList (X,[]) = X
| multiList ([],X) = X
| multiList (x::xs, y::ys) = (x*y ::multiList(xs,ys));
当列表为空时,此代码返回非空的
Edit:正如@ruakh在评论中所说的,下面的代码更好,因为它似乎是multiList
的自然行为,但我将把上面的代码留作解释
fun
multiList ([],[]) = []
| multiList (x::xs, y::ys) = (x*y ::multiList(xs,ys))
| multiList _ = raise Fail "lists of non equal length";
请注意,
是通配符,因此当([],[])
和(x::xs,y::ys)
都不匹配时,它会匹配任何内容。+1,尽管我怀疑预期的行为实际上类似于多重列表
。谢谢您,先生!我刚刚制作了| multiList(X,[])=raise DifferentLength | multiList([],X)=raise DifferentLength
,这引发了一个类似的异常+1,尽管我怀疑预期的行为实际上类似于| multiList=>raise Fail“长度不相等的列表”
。谢谢您,先生!我刚刚制作了| multiList(X,[])=raisedifferentlength | multiList([],X)=raisedifferentlength
,这引发了类似的异常