sml模式不会看到绑定变量

时间:2014-10-16 17:47:04

标签: pattern-matching sml

我正在做家庭作业。任何人都可以解释为什么这段代码有效:

  fun remove_card(cs: card list, c: card, except:exn)=
case cs of []=>raise except
 | x::xs =>( case (x=c) of true=> xs
                | _ => x::remove_card(xs,c,except))

但是在这里说"模式冗余"好像c未定义?

    fun remove_card(cs: card list, c: card, except:exn)=
case cs of []=>raise except
 | x::xs =>( case x of c=> xs
                | _ => x::remove_card(xs,c,except))

另一件有用的事情就是改为" cs为x :: xs"在函数声明中,但为什么它有帮助?

1 个答案:

答案 0 :(得分:0)

cs as x::xs绑定cs并假设它是非空的,将cs的头部绑定到x,将尾部绑定到xs。它只是一个方便的速记。

你的例子中有一个拼写错误,但如果你的意思是

case x of c => xs 

然后你将x绑定到x,这总是成功的。