我有在SML两个列表,可以说,名单A [(a,b,c),(d,e,f)]和B名单[b,e].我想计算B中与B中每个三元组的第二个元素匹配的每个项目的出现次数.输出应该是2.因为b并且e每个在A中出现一次.
到目前为止这是我的代码,但是当我从B中的一个元素移动到另一个元素时,我的计数器总是设置为0.我知道在Java中这只是一个简单的双循环.
fun number_in_months (d : (int * int * int ) list, m : (int) list) =
if null m then 0
else if null d then number_in_months(d, tl m)
else if (#2(hd d)) = (hd m) then 1 + number_in_months (tl d, m)
else number_in_months(tl d, m)
Run Code Online (Sandbox Code Playgroud) 我是标准ML的新手,无法弄清楚为什么我会遇到此类型不匹配错误:
fun number_in_month (month : int, dates : int list) =
if null dates
then 0
else if (month = (hd (tl (hd dates))))
then number_in_month(month, (tl dates)) + 1
else number_in_month(month, (tl dates))
Run Code Online (Sandbox Code Playgroud)
评估此函数会导致以下错误:
Error: operator and operand don't agree [tycon mismatch]
5 operator domain: 'Z list
6 operand: int
7 in expression:
8 tl (hd dates)
Run Code Online (Sandbox Code Playgroud)
但是,在REPL中,如果我执行以下操作:
val x = [[84, 12, 23], [83, 01, 18]]
12 = (hd (tl (hd x))) (* -> val it = true …Run Code Online (Sandbox Code Playgroud)