ligo/src/test/contracts/loop.ligo

68 lines
1.6 KiB
Plaintext
Raw Normal View History

// Test while loops in PascaLIGO
2019-05-13 00:56:22 +04:00
function counter (var n : nat) : nat is block {
var i : nat := 0n ;
while (i < n) block {
i := i + 1n ;
}
} with i
function while_sum (var n : nat) : nat is block {
2019-05-13 00:56:22 +04:00
var i : nat := 0n ;
var r : nat := 0n ;
while (i < n) block {
i := i + 1n ;
r := r + i ;
}
} with r
2019-10-22 14:12:19 +04:00
function for_sum (var n : nat) : int is block {
2019-10-11 20:31:04 +04:00
var acc : int := 0 ;
2019-10-22 14:12:19 +04:00
for i := 1 to int(n)
begin
2019-10-11 20:31:04 +04:00
acc := acc + i ;
end
} with acc
2019-10-22 14:12:19 +04:00
function lamby (var accu : (record st : string ; acc : int end) ; var i : int )
: (record st : string ; acc : int end) is block {
accu.acc := accu.acc + i ;
accu.st := accu.st ^ "to" ;
} with accu
2019-10-27 14:47:17 +04:00
function for_collection_ (var nee : unit; var nuu : unit) : (int * string) is block {
2019-10-11 20:31:04 +04:00
var acc : int := 0 ;
2019-10-22 14:12:19 +04:00
var st : string := "to" ;
var mylist : list(int) := list 1 ; 1 ; 1 end ;
2019-10-22 14:12:19 +04:00
var init_record : (record st : string; acc : int end ) :=
record st = st; acc = acc; end;
var folded_record : (record st : string; acc : int end ) :=
list_fold(mylist , init_record , lamby) ;
skip ;
st := folded_record.st ;
acc := folded_record.acc ;
2019-10-22 14:12:19 +04:00
} with (folded_record.acc , folded_record.st)
2019-10-27 14:47:17 +04:00
function for_collection (var nee : unit) : (int * string) is block {
var acc : int := 0 ;
var st : string := "to" ;
2019-10-27 00:16:17 +04:00
// var toto : (string * string) := ("foo","bar") ;
var mylist : list(int) := list 1 ; 1 ; 1 end ;
2019-10-22 14:12:19 +04:00
for x : int in list mylist
begin
2019-10-27 00:16:17 +04:00
// toto.1 := "r";
acc := acc + x ;
st := st^"to" ;
end
2019-10-22 14:12:19 +04:00
2019-10-27 14:47:17 +04:00
} with (acc, st)
2019-10-15 15:14:00 +04:00
2019-05-13 00:56:22 +04:00
function dummy (const n : nat) : nat is block {
while (False) block { skip }
} with n