From bd116d8810bf765ac2a1a2e754de2d354bc8f79b Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Mon, 19 Sep 2022 22:16:38 +0200 Subject: [PATCH] Improved factorial example --- examples/factorial.mq | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/examples/factorial.mq b/examples/factorial.mq index c896995..0954d71 100644 --- a/examples/factorial.mq +++ b/examples/factorial.mq @@ -1,15 +1,30 @@ -for := [ start stop iteration | - if (start > stop) - [nil] - [iteration start; for (start + 1) stop iteration] +---------------------------------------------------------------------------- +This example shows how to implement factorial operation using different +implementation techniques, showing how one can iterate and accumulate +using Musique programming language +---------------------------------------------------------------------------- + +-- Calculate factorial using recursive approach +factorial_recursive := [n | + if (n <= 1) + [1] + [n * (factorial_recursive (n-1))] ]; -factorial := [n | if (n <= 1) [1] [n * (factorial (n-1))]]; -for 1 10 [i | say (factorial i)]; - +-- Calculate factorial using iterative approach factorial_iterative := [n | x := 1; - for 1 n [i|x *= i]; + for (range 1 (n+1)) [i|x *= i]; x ]; -for 1 10 [i | say (factorial_iterative i)]; + +-- Calculate factorial using composition of functions +factorial := [n| fold (1 + up n) 1 '*]; + +-- Gather all functions into array, and iterate over it +-- This allows to reduce repeatition of this test case +for [factorial_recursive; factorial_iterative; factorial] [ factorial | + for (up 10) [ n | + say (factorial (n)); + ]; +];