0

I'm running this program in Dr. Racket using R5RS scheme and am getting this error on the line (+ 1 IntDivide((- x y) y)):

"application: not a procedure; expected a procedure that can be applied to arguments given: 5 arguments...:"

The procedure is supposed to return the quotient of the division between two integers using subtraction. Since this is a homework problem, I'm not going to ask whether my solution is correct (I can debug that later), but rather what is causing this error. It seems to be commonly caused by excess brackets, but I can't seem to find them. Any help would be appreciated.

(define IntDivide (lambda (x y)
  (if (eqv? (integer? x) (integer? y))
    (begin
      (if (= y 0)
        (begin
          (write "Can't divide by zero") (newline)
          -1
        )
      )

      (if (= (- x y) 0)
        1
      )

      (if (< x y)
        0
      )

      (if (> x y)
        (+ 1 IntDivide((- x y) y))
      )
    )
  )
  (write "Please only input integers")
))

Thanks in advance!

Knight Artorias
  • 105
  • 1
  • 6

2 Answers2

3

In addition to moving the operator inside the parens, you also need to replace the if with a cond:

(define IntDivide
  (lambda (x y)
    (if (eqv? (integer? x) (integer? y))
      (cond ((= y 0) (write "Can't divide by zero")
                     (newline)
                     -1)
            ((= x y) 1)
            ((< x y) 0)
            ((> x y) (+ 1 (IntDivide (- x y) y))))
      (write "Please only input integers"))))

The way you have it now, with the interior if expressions, won't work because they don't automatically return. They just evaluate and then the result gets thrown away.

Brendan Cannell
  • 679
  • 4
  • 11
2

Call IntDivide the same way you would any other function.

(+ 1 (IntDivide (- x y) y))
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • Major face palm moment there. Thanks for the quick response. – Knight Artorias Apr 09 '17 at 20:40
  • 1
    @KnightArtorias I can't tell you how many times I've done this when going back and forth between Scheme and other languages. – Bill the Lizard Apr 09 '17 at 20:41
  • @KnightArtorias Also, you do need to switch to a `cond` structure as mentioned in the answer by Brendan. I ran your code with just this fix, and it returns `""Please only input integers"` for most inputs. – Bill the Lizard Apr 09 '17 at 20:51
  • 1
    @BilltheLizard When I try to help someone with python, I always start everything with a `(`. It's hard to revert. – Carcigenicate Apr 09 '17 at 23:43