How to create a function that multiplies all numbers between 1 and "x" by bunkers?

I am making a function that multiplies all the numbers between input 1 and "x" with a point loop. If you want, check my function and say what is wrong, since I do not know the loops on the Scheme very well.

(define (product x)
  (let ((result 1))
        (dotimes (temp x)
                 (set! result (* temp (+ result 1))))
    result))
+3
source share
1 answer

Use recursion. This is a way to do something in Scheme / Racket. And try never to use set!other functions that change variables, if there is no other choice.

Here is an example of a recursion tutorial in a schema:

(define factorial
  (lambda (x)
    (if (<= x 1)
        1
        (* x (factorial (- x 1))))))
+5
source

All Articles