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)))))
\$\endgroup\$
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)))
\$\endgroup\$
  • \$\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 Jun 10 '11 at 19:38
  • \$\begingroup\$ @gn66: It returns 6 for me. Notice that I updated the function. \$\endgroup\$ – Chris Jester-Young Jun 10 '11 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy