Vic*_*uiz 0 functional-programming ml sml pattern-matching
我在SML上写这个函数.它应该列出可能的名字变体(我的名字是Victoria,所以V,Vic,Vicky等)并创建{altname1,middle,last},{alt2,middle,last}的记录.
所以这是我的代码:
fun similar_names (substits:, name) =
let
val {first=n1, second=n2, third=n3} = name
fun name_constructor (altnames:string list, acc) =
case altnames of
[] => acc
| a::aa => {first=a, second=n2, third=n3}::acc
in
name_constructor( get_substitutions2(substits, n1),name)
end
Run Code Online (Sandbox Code Playgroud)
get_substitutions2只会给出一个名字的所有可能变体的列表(即:字符串列表),并且它可以工作.
我得到的错误是:
a02.sml:65.2-65.58 Error: operator and operand don't agree [tycon mismatch]
operator domain: string list * {first:string, second:'Z, third:'Y} list
operand: string list * {first:string, second:'Z, third:'Y}
in expression:
name_constructor (get_substitutions2 (substits,n1),name)
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它只在记录列表和记录之间进行.你能帮忙吗?
name
只有一个记录,但name_constructor
希望acc
是一个列表(因为你说::acc
).
尝试
name_constructor(get_substitutions2(substits, n1), [name])
Run Code Online (Sandbox Code Playgroud)