为什么会出现这些错误,我该如何解决?

问题描述

我正在用Standard ML写一个解释器,但是我在此函数中的语法有麻烦,而且我无法弄清楚出了什么问题。

以下是相关代码:

| eval (rho,SetExp (name,value)) =
    (case rhoContains rho name of
        true    => rhoSet rho name value (rho,value)
    |   false   => globalSet (name,value))


fun rhoSet [] key value = [(key,value)]
  | rhoSet ((elt as (k,v)) :: tail) key value =
        if key = k then (key,value) :: tail else elt :: rhoSet tail key value


fun rhoContains rho name =
    case rhoGet rho name of SOME _ => true | NONE => false

这是SetExp的来源:

datatype expression =
    SetExp of (string * expression)

运行此命令会给我列出很多错误,但是我认为这是相关的部分。第62行是true中以eval开头的行:

eval.sml:62: error: Type error in function application.
  Function: rhoSet rho name value : (string * expression) list
  Argument: (rho,value) : (string * expression) list * expression
  Reason: Value being applied does not have a function type

解决方法

您要向rhoSet传递太多参数-请删除尾随对。

| eval (rho,SetExp (name,value)) =
    (case rhoContains rho name of
        true    => rhoSet rho name value
    |   false   => globalSet (name,value))

您还可以通过条件使此内容更具可读性:

| eval (rho,value)) =
    if rhoContains rho name
    then rhoSet rho name value
    else globalSet (name,value)
,

对于SML,在表达式rhoSet rho name value (rho,value)中,rhoSet rho name value是一个函数,而(rho,value)是该函数的自变量。但是,根据rhoSet的定义,rhoSet rho name value必然是一个列表,而不是一个函数。这就是错误消息的含义。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...