Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use infix operator from a SML module?

Tags:

smlnj

I have the following to code and I use 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

I want to use the ++ operator from the interpreter but when I write s1 List.++ s2 where s1 and s2 stack types I get the message that operator is not a function.

Thanks.

like image 962
Dragno Avatar asked Jan 02 '13 20:01

Dragno


People also ask

What is an infix operator in Python?

Infix operators are used in between two operands, so simple arithmetic operations such as 1 + 2 would be an infix expression where + is the infix operand. Prefix and postfix operators are either applied before or after a single operand.

What is an infix operator in R?

An infix operator allows a function to be called between two given arguments. A postfix operator just places the arguments prior to the function operation.

What is an infix function in Haskell?

Functions in Haskell default to prefix syntax, meaning that the function being applied is at the beginning of the expression rather than the middle. Operators are functions which can be used in infix style.


1 Answers

You've declared ++ as infix inside the structure, and that declaration is restricted to the scope of the structure (inside struct...end). You can declare it as infix at the top-level, or use it as prefix, but in SML infix declarations aren't part of the signature.

- 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

Check this out for some interesting hacks: http://www.mlton.org/InfixingOperators

like image 163
michiakig Avatar answered Oct 05 '22 23:10

michiakig