你如何对负 1 进行模式匹配?

How do you pattern-match against negative 1?

这失败并显示“the pattern is expected to be formed with a constructor (of datavtype)”,因为它匹配线性构造函数的特殊 free()ing 语法而不是负号。

#include "share/atspre_staload.hats"
implement main0() =
        case ~1 of
        | ~1 => println!("ok")
        | _ => ()

失败并显示“operator fixity cannot be resolved”。添加括号或创建二元运算符都无济于事。

#include "share/atspre_staload.hats"
implement main0() =
        case ~1 of
        | -1 => println!("ok")
        | _ => ()

这失败了,因为 case 中的变量引入了一个新的绑定,与范围内已有的绑定无关:

#include "share/atspre_staload.hats"
implement main0() =
        let
                val negative_one = ~1
        in
                case ~2 of
                | negative_one => println!("unintend match")
                | _ => () // error: this pattern match clause is redundant
        end

这是我们能得到的最接近的吗?性能差了多少?

#include "share/atspre_staload.hats"
implement main0() =
        let
                val negative_one = ~1
        in
                ifcase
                | negative_one = ~1 => println!("this works")
                | _ => ()
        end

在ATS中,~1不被认为是常数;它是一个函数应用:~被应用到1。你可以定义负1如下:

#define NEG1 %(~1) // not yet working // to be fixed

#define NEG1 %(0-1) // this one is currently working

那你可以这样写:

case ~1 of
| 1 => ...
| NEG1 => ...

使用 'ifcase'(而不是 'case')应该同样有效。