错误:字符串列表而不是字符串列表

时间:2013-01-31 00:55:26

标签: sml

我有这个函数导致字符串列表:

fun get_substitutions1 ([],_) = []
| get_substitutions1 (x::xs,s) = case all_except_option(s,x) of
    NONE     => []  @get_substitutions1(xs,s)
  | SOME lst => lst @get_substitutions1(xs,s)

这个函数采用字符串列表和类型:

fun similar_names(slist,full_name:{first:string,middle:string,last:string})=
let
fun aux(slist,acc)=
case full_name of
{first=a,middle=b,last=c} => case get_substitutions1(slist,a) of
[] => full_name::acc
| x::xs'  => full_name::  aux(xs',{first=x,middle=b,last=c}::acc)

in aux(slist,[])
end

我收到一个错误:

Error: operator and operand don't agree.

operator domain: string list list * 
                 {first:string, last:string, middle:string} list
operand:         string list * 
                 {first:string, last:string, middle:string} list
in expression:
   aux (xs',{first=x,middle=b,last=c} :: acc)

还有其他办法吗?

1 个答案:

答案 0 :(得分:4)

首先,您可能不想缩进代码以使其可读。

很明显,为什么你会收到你所犯的错误。功能

fun get_substitutions1 ([],_) = []
  | get_substitutions1 (x::xs,s) =
    case all_except_option(s,x) of
      NONE => []@get_substitutions1(xs,s)
    | SOME lst => lst @get_substitutions1(xs,s)

的类型为

val get_substitutions1 = fn : ''a list list * ''a -> ''a list

并且您尝试在内部case表达式中使用此函数的结果,其中您获取返回列表的尾部(类型'a list)并在递归函数调用中使用它们。

fun similar_names(slist,full_name:{first:string,middle:string,last:string})=
    let
      fun aux(slist,acc)=
          case full_name of
            {first=a,middle=b,last=c} =>
            case get_substitutions1(slist,a) of
              [] => full_name::acc
            | x::xs'  => full_name::  aux(xs',{first=x,middle=b,last=c}::acc)
    in aux(slist,[])
    end

但是,由于aux中使用了get_substitutions1的第一个参数,因此该参数必须是'a list list类型,但在递归调用中使用的xs'是仅属于'a list类型。