From 2eaeb269debb4f610caa8927dbf91d070ba1c4e7 Mon Sep 17 00:00:00 2001 From: Milton Date: Wed, 21 Jun 2023 12:34:34 -0300 Subject: [PATCH 1/2] Recursion --- content/examples/recursion.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 content/examples/recursion.md diff --git a/content/examples/recursion.md b/content/examples/recursion.md new file mode 100644 index 0000000..bdeff5f --- /dev/null +++ b/content/examples/recursion.md @@ -0,0 +1,24 @@ +--- +title: "recursion" +date: 2023-06-21T14:00:01+0000 +draft: false +--- + +Cairo uses recursion as the principal method of iteration. + +Here is how we can calculate the factorial using this method. + +```rust {.codebox} +// The IsZeroResult enum allows us to obtain a NonZero via matching. + +fn main() -> felt252 { + factorial(9); +} + +fn factorial(value: felt252) -> felt252 { + match value { + 0 => 1, + _ => value * factorial(value-1), + } +} +``` \ No newline at end of file From 27d56163a8eb8b31a567ed1cf6b086d0737fccf5 Mon Sep 17 00:00:00 2001 From: Milton Date: Wed, 21 Jun 2023 14:23:14 -0300 Subject: [PATCH 2/2] Delete unnecessary comment --- content/examples/recursion.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/examples/recursion.md b/content/examples/recursion.md index bdeff5f..0bfca42 100644 --- a/content/examples/recursion.md +++ b/content/examples/recursion.md @@ -9,7 +9,6 @@ Cairo uses recursion as the principal method of iteration. Here is how we can calculate the factorial using this method. ```rust {.codebox} -// The IsZeroResult enum allows us to obtain a NonZero via matching. fn main() -> felt252 { factorial(9);