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
end
structure List : STACK =
structtypeaStack = 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
end
structure CustomStack : STACK =
struct
datatype a Stack = NIL
| CONS of a * a Stack
val empty = NIL
fun isEmpty NIL = true
| isEmpty _ = false
fun cons (x, s) = CONS (x, s)
fun head NIL = raiseEMPTY
| head (CONS (x, s)) = x
fun tail NIL = raiseEMPTY
| tail (CONS (x, s)) = s
end
fun [] ++ ys = ys
| (x :: xs) ++ ys = x :: (xs ++ ys)
fun update ([], i, y) = raise SUBSCRIPT
| update (x :: xs, 0, y) = y :: xs
| update (x :: xs, i, y) = x :: update(xs, i - 1, y)
signature STACKsig 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 Stackendstruct