Chez Scheme Factorial Program | Generated by AI
Here’s a simple Chez Scheme program that calculates the factorial of a number. Save it as factorial.scm
:
(define (factorial n)
(if (<= n 1)
1
(* n (factorial (- n 1)))))
(display "Factorial of 5 is: ")
(display (factorial 5))
(newline)
To run it:
- Save the code in a file named
factorial.scm
. - In your terminal, navigate to the directory containing the file.
- Run:
scheme --script factorial.scm
This will output:
Factorial of 5 is: 120
The program defines a recursive factorial
function and computes the factorial of 5. The --script
option (as shown in the scheme --help
output) allows you to run the file directly.