Introduction

Letlang provides only one loop mechanism, the tailrec functions.

A tail recursive function returns either:

  • final[value] to exit the loop
  • recurse[args...] to keep iterating with new arguments

Syntax:

<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>
    ;

Example

tailrec len<T>(items: list<T>, acc: int) -> int {
  match items {
    [] => final[acc],
    [_ | tail] => recurse[tail, acc + 1],
  };
}