-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.sml
34 lines (28 loc) · 662 Bytes
/
stack.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 Stack : STACK =
struct
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
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 s = case s of Nil => raise Empty | Cons(h,_) => h
fun tail s = case s of Nil => raise Empty | Cons(_,t) => t
end