sophia/test/contracts/factorial.aes
Ulf Norell 5aed8b3ef5 Check stateful annotations
Functions must be annotated as `stateful` in order to
- Update the contract state (using `put`)
- Call `Chain.spend` or other primitive functions that cost tokens
- Call an Oracle or AENS function that requires a signature
- Make a remote call with a non-zero value
- Construct a lambda calling a stateful function

It does not need to be stateful to
- Read the contract state
- Call another contract with value=0, even when the remote function is stateful
2019-05-13 13:39:17 +02:00

18 lines
470 B
Plaintext

// An implementation of the factorial function where each recursive
// call is to another contract. Not the cheapest way to compute factorial.
contract FactorialServer =
function fac : (int) => int
contract Factorial =
record state = {worker : FactorialServer}
function init(worker) = {worker = worker}
stateful function set_worker(worker) = put(state{worker = worker})
function fac(x : int) : int =
if(x == 0) 1
else x * state.worker.fac(x - 1)