/ test / contracts / sophia_2 / stack.aes
stack.aes
 1  // Testing more interesting state types
 2  contract Stack =
 3  
 4    type stack('a) = list('a)
 5  
 6    record state = { stack : stack(string),
 7                     size  : int }
 8  
 9    function init(ss : list(string)) = { stack = ss, size = length(ss) }
10  
11    private function length(xs) =
12      switch(xs)
13        [] => 0
14        _ :: xs => length(xs) + 1
15  
16    stateful function pop() : string =
17      switch(state.stack)
18        s :: ss =>
19          put(state{ stack = ss, size = state.size - 1 })
20          s
21  
22    stateful function push(s) =
23      put(state{ stack = s :: state.stack, size = state.size + 1 })
24      state.size
25  
26    function all() = state.stack
27  
28    function size() = state.size
29