如何从SML模块使用中缀操作符?

时间:2013-01-02 20:38:00

标签: smlnj

我有以下代码,我使用SML / NJ:

signature STACK=
sig

    type 'a Stack

    val empty :'a Stack
    val isEmpty : 'a Stack -> bool

    val cons : 'a*'a Stack -> 'a Stack
    val head : 'a Stack ->'a
    val tail : 'a Stack -> 'a Stack
    val ++ : 'a Stack * 'a Stack -> 'a Stack
end
structure List : STACK = 
 struct
 infix 9 ++
type 'a Stack = 'a list

val empty = []
fun isEmpty s = null s

fun cons (x,s) = x::s
fun head s = hd s
fun tail s = tl s
fun xs ++ ys = if isEmpty xs then ys else cons(head xs, tail xs ++ ys)    

end

我想使用解释器中的++运算符但是当我写s1 List。++ s2其中s1和s2堆栈类型时,我得到运算符不是函数的消息。

感谢。

1 个答案:

答案 0 :(得分:3)

您已将++声明为结构中的中缀,并且该声明仅限于结构的范围(在struct...end内)。您可以在顶级将其声明为中缀,或将其用作前缀,但在SML中缀声明中不是签名的一部分。

- List.++ ([1], [2,3]);
val it = [1,2,3] : int Stack

- infix 9 ++;
infix 9 ++
- open List;
...
- [1] ++ [2,3];
val it = [1,2,3] : int Stack

查看一些有趣的黑客:http://www.mlton.org/InfixingOperators