SML:带有常量的模式?

问题描述 投票:0回答:1

我正在尝试在 SML 中编写模式匹配函数,但出现此错误

Error: non-constructor applied to argument in pattern: -

我正在尝试做这样的事情:

fun illegal (c:char) i j n m =
    let
        fun legal (#"A") 0 j = true
           |legal (#"B") i 0 = true
           |legal (#"C") i n = true
           |legal (#"D") m j = true
           | legal c  i j     = false
     in
        if (legal c i j = false) then 0
        else i
     end

**I suspect that the problem is that my n,m are two constants that have been valued right before. Any help appreciated
(I searched online and here I added parentheses in my chars, and tried some other stuff but my error persisted)** 
sml smlnj
1个回答
1
投票

当您进行模式匹配时,像

i
这样的模式不会检查所绑定的表达式是否与现有
i
绑定具有相同的值,而是引入了一个新的
i
绑定到新表达式,这会遮蔽先前定义的绑定。

当你有:

legal (#"A") 0 j = true

您真正在寻找的是这样的东西:

legal (#"A") 0 j2 = j = j2
© www.soinside.com 2019 - 2024. All rights reserved.