Factorial in Elvish

Published on 12 January 2025 (Updated: 12 January 2025)

Welcome to the Factorial in Elvish page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

if (> 1 (count $args)) {
  echo "Usage: please input a non-negative integer"
  exit 1
}

try {
  var r = (+ $args[0] 1) # Hacky, but it tries to add the argument with one.
} catch { # If it fails, the value is not a number.
  echo "Usage: please input a non-negative integer"
  exit 1
}

if (> 0 $args[0]) {
  echo "Usage: please input a non-negative integer"
  exit 1
}

var f = 1

for i [(range 1 (+ 1 $args[0]))] {
  set f = (* $f $i)
}

echo $f

Factorial in Elvish was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.