3.2. Loops
Overview of Letlang's looping mechanism
Letlang provides only one loop mechanism, the tailrec
functions.
A tail recursive function returns either:
final[value]
to exit the looprecurse[args...]
to keep iterating with new argumentsSyntax:
<tailrec-statement> :=
[ "pub" ] "tailrec" <identifier>
[ <tailrec-type-params> ]
"(" [ <tailrec-call-params> ] ")"
"->" <type-ref> "{"
<proposition>+
"}"
;
<tailrec-type-params> :=
"<" <identifier> ("," <identifier>)* ">"
;
<tailrec-call-params> :=
<tailrec-call-param> ("," <tailrec-call-param>)*
;
<tailrec-call-param> :=
<identifier> ":" <type-ref>
;
tailrec len<T>(items: list<T>, acc: int) -> int {
match items {
[] => final[acc],
[_ | tail] => recurse[tail, acc + 1],
};
}