1
\$\begingroup\$

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
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\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\$ Commented Jun 10, 2011 at 19:38
  • \$\begingroup\$ @gn66: It returns 6 for me. Notice that I updated the function. \$\endgroup\$ Commented Jun 10, 2011 at 19:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.