interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / ackermann-function / ackermann-function.om

57 lines · 1044 bytes Raw Blame History
1note Rosetta Code task "Ackermann function": https://rosettacode.org/wiki/Ackermann_function
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text pre "A("
6text cm ", "
7text post ") = "
8note emit writes every byte of a to stdout; a short write is retried, an error exits 1
9fn emit a view uchar * to void
10o = ulong 0
11while lt o len a bound 4096
12k = write 1 a o sub len a o
13if le k 0
14exit 1
15end
16o = add o ulong k
17end
18end
19note emitu writes n in decimal; 20 digits hold every ulong
20fn emitu n ulong to void
21d = new uchar 20 0
22i = ulong 20
23v = n
24while true bound 20
25i = sub i 1
26put d i uchar add rem v 10 48
27v = div v 10
28if eq v 0
29break
30end
31end
32emit part d i sub 20 i
33end
34note recursive, as the task asks; program mode allows recursion, the 8 MiB stack bounds its depth
35fn ack m ulong n ulong to ulong
36if eq m 0
37ret add n 1
38end
39if eq n 0
40ret ack sub m 1 1
41end
42ret ack sub m 1 ack m sub n 1
43end
44fn main to int
45for m ulong 4
46for n ulong 5
47emit pre
48emitu m
49emit cm
50emitu n
51emit post
52emitu ack m n
53emit nl
54end
55end
56ret 0
57end