3
votes

I'm creating a fuction that actually does the following:

(mateval '(ADD 3 (MUL 2 5))
13
(mateval '(MOD 10 (MUL (DIV 8 4) (SUB 5 3))))
2

Or basically do the maths there using ADD instead of + and so. Problem is when I call my main function and it uses the MOD(remainder) one

(mod
 (lambda (exp)
  (cond((null? exp) null)
       ((null? (cdr exp)) null)
       (else(remainder (init (car exp))(init (cdr exp)))))))

It shows the following problem:

remainder: contract violation

expected: integer?

given: '(MUL (DIV 8 4) (SUB 5 3))

argument position: 2nd

other arguments.:

Init is the function that calls the others when it's needed:

(init
 (lambda (exp)
  (cond((not(pair? exp)) exp)
       ((null? exp) null)
       ((null? (cdr exp))(car exp))
       ((eqv? 'ADD (car exp))(add (cdr exp)))
       ((eqv? 'SUB (car exp))(sub (cdr exp)))
       ((eqv? 'MUL (car exp))(mul (cdr exp)))
       ((eqv? 'DIV (car exp))(div (cdr exp)))
       ((eqv? 'MOD (car exp))(mod (cdr exp)))
       (else null))))

The others functions have similar code, so it seems it's only the remainder(tried modulo as well) function the one creating trouble. Tried many times, not a syntax error, I'm even putting the same in the first argument and it only crashes at the 2nd no idea of what's going on, It seems It just want to make the 2nd argument to crash. Instead of using (cdr exp) in "init" it just throw (cdr exp) back and crash. ¿Any ideas? Thanks for reading.

1

1 Answers

1
votes

In the last line you need to access the cadr of the expression, not the cdr - please try this:

(mod
 (lambda (exp)
   (cond ((null? exp) null)
         ((null? (cdr exp)) null)
         (else (remainder (init (car  exp))
                          (init (cadr exp)))))))