17 end; |
19 end; |
18 |
20 |
19 structure Stack: STACK = |
21 structure Stack: STACK = |
20 struct |
22 struct |
21 |
23 |
22 type 'a T = 'a * 'a list; |
24 abstype 'a T = Stack of 'a * 'a list |
|
25 with |
23 |
26 |
24 fun level (_, xs) = length xs; |
27 fun make x xs = Stack (x, xs); |
|
28 fun dest (Stack (x, xs)) = (x, xs); |
25 |
29 |
26 fun init x = (x, []); |
30 fun level (Stack (_, xs)) = length xs; |
27 |
31 |
28 fun top (x, _) = x; |
32 fun init x = Stack (x, []); |
29 |
33 |
30 fun map_top f (x, xs) = (f x, xs); |
34 fun top (Stack (x, _)) = x; |
31 |
35 |
32 fun map_all f (x, xs) = (f x, map f xs); |
36 fun map_top f (Stack (x, xs)) = Stack (f x, xs); |
33 |
37 |
34 fun push (x, xs) = (x, x :: xs); |
38 fun map_all f (Stack (x, xs)) = Stack (f x, map f xs); |
35 |
39 |
36 fun pop (_, x :: xs) = (x, xs) |
40 fun push (Stack (x, xs)) = Stack (x, x :: xs); |
37 | pop (_, []) = raise List.Empty; |
41 |
|
42 fun pop (Stack (_, x :: xs)) = Stack (x, xs) |
|
43 | pop (Stack (_, [])) = raise List.Empty; |
38 |
44 |
39 end; |
45 end; |
|
46 |
|
47 end; |