Skip to content

Commit

Permalink
doc: recursion factorial
Browse files Browse the repository at this point in the history
  • Loading branch information
artegoser committed Aug 18, 2022
1 parent 6dbc9f7 commit 19fc8fa
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions examples/factorial.onla
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
[
{
fn: {
name: "fact",
name: "fact_loop",
args: ["n"],
body: [
{ let: { result: { var: "fact.n" } } },
{ let: { result: { var: "n" } } },
{
loop: [
//using loop because recursion is not supported
//using loop because recursion is not supported*
//* - already supported
{
if: {
condition: { comp: [{ var: "fact.n" }, ">", 1] },
condition: { comp: [{ var: "n" }, ">", 1] },
body: [
{
assign: {
"fact.n": {
calc: [{ var: "fact.n" }, "-", 1],
n: {
calc: [{ var: "n" }, "-", 1],
},
},
},
{
assign: {
result: {
calc: [{ var: "result" }, "*", { var: "fact.n" }],
calc: [{ var: "result" }, "*", { var: "n" }],
},
},
},
Expand All @@ -37,11 +38,37 @@
},
},

{
fn: {
name: "fact",
args: ["n"],
body: [
{
if: {
condition: { comp: [{ var: "n" }, "==", 1] },
body: [{ return: 1 }],
else: [
{
return: {
calc: [
{ var: "n" },
"*",
{ fact: [{ calc: [{ var: "n" }, "-", 1] }] },
],
},
},
],
},
},
],
},
},

{
fn: {
name: "_eq",
args: ["a", "b"],
body: [{ return: { comp: [{ var: "_eq.a" }, "==", { var: "_eq.b" }] } }],
body: [{ return: { comp: [{ var: "a" }, "==", { var: "b" }] } }],
},
},

Expand Down Expand Up @@ -70,6 +97,10 @@
"20! == 2 432 902 008 176 640 000: ",
{ _eq: [{ fact: [20] }, 2432902008176640000] },
],
"",
["(loop) 10! == 3 628 800: ", { _eq: [{ fact_loop: [10] }, 3628800] }],
["(loop) 11! == 39 916 800: ", { _eq: [{ fact_loop: [11] }, 39916800] }],

// [
// "21! == 51 090 942 171 709 440 000: ",
// { _eq: [{ fact: [21] }, 51090942171709440000] },
Expand Down

0 comments on commit 19fc8fa

Please sign in to comment.