tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

64 lignes · 778 octetsBrut · Historique
1// Tail calls run in constant stack, on both backends: a million calls
2// with a small stack (`//@ small-stack`, tests/README.md).
3//@ small-stack
4
5import std/io
6
7fn count :: n: Int acc: Int -> Int
8{
9 if n == 0
10 {
11 acc
12 }
13 else
14 {
15 count (n - 1) (acc + 1)
16 }
17}
18
19fn even :: n: Int -> Bool
20{
21 if n == 0
22 {
23 True
24 }
25 else
26 {
27 odd (n - 1)
28 }
29}
30
31fn odd :: n: Int -> Bool
32{
33 if n == 0
34 {
35 False
36 }
37 else
38 {
39 even (n - 1)
40 }
41}
42
43/// Through a `match` and a `let`: still tail calls.
44fn walk :: n: Int -> Int
45{
46 match n
47 { 0 -> 0
48 , _ ->
49 {
50 let next = n - 1
51 walk next
52 }
53 }
54}
55
56pub fn main :: -> () / <io>
57{
58 io.println "{count 1000000 0}"
59 io.println "{even 1000001}"
60 io.println "{walk 1000000}"
61}
62//> 1000000
63//> False
64//> 0