\$\begingroup\$
\$\endgroup\$
I'm making a function that multiplies all numbers between an "a" input and a "b" input with do
loop. If you please, check my function and say what's wrong since I don't know loops very well in Scheme.
(define (pi-function x y)
(let ((result y))
(do ((limI x (+ x 1)))
((= limI y) result)
(set! result (* result limI)))))
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jun 10, 2011 at 19:15
gn66gn66
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
You can use a do
loop without using set!
.
(define (product-of-range x y)
(do ((result 1 (* result i))
(i x (+ i 1)))
((> i y) result)))
answered Jun 10, 2011 at 19:32
-
\$\begingroup\$ But that doesn't work, when I call (product 1 3) it returns 3, but it should return 6. That function only multiply x for y. What should do, f.e. (product 1 3) (* 1 2) (* 2 3) \$\endgroup\$gn66– gn662011年06月10日 19:38:12 +00:00Commented Jun 10, 2011 at 19:38
-
\$\begingroup\$ @gn66: It returns 6 for me. Notice that I updated the function. \$\endgroup\$C. K. Young– C. K. Young2011年06月10日 19:38:47 +00:00Commented Jun 10, 2011 at 19:38
lang-lisp