首页 文章

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

提问于
浏览
1

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

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 回答

  • 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
    

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

相关问题