2


It is impossible to rewrite the code from Lisp language to Scheme language, an error occurs, help to correct.
For example (cadr x) is equivalent to (car (cdr x)) a function (cadr x) is equivalent to (car (cdr x)), and I don't know how to interpret the numberp function.
Lisp code:

(defun deriv (f x)
  (cond ((numberp f) 0)
        ((eq f x) 1)
        ((eq (car f) '+) `(+ ,(deriv (cadr f) x) ,(deriv (caddr f) x)))
        ((eq (car f) '-) `(- ,(deriv (cadr f) x) ,(deriv (caddr f) x)))
        ((eq (car f) '*) `(+ (* ,(caddr f) ,(deriv (cadr f) x)) (* ,(cadr f) ,(deriv (caddr f) x))))
        ((eq (car f) '^) `(* (* ,(caddr f) (^ ,(cadr f) (- ,(caddr f) 1))) ,(deriv (cadr f) x)))))

How I tried to write this code on Scheme:

(define deriv (f x)
  (cond
        ((eq? f x) 1)
        ((eq? (car f) '+) '(+ ,(deriv (car (cdr f) x)) ,(deriv (car (cdr (cdr f) x)))))
        ((eq? (car f) '-) '(- ,(deriv (car (cdr f) x)) ,(deriv (car (cdr (cdr f) x)))))
        ((eq? (car f) '*) '(+ (* ,(car (cdr (cdr f))) ,(deriv (car (cdr f) x))) (* ,(car (cdr f)) ,(deriv (car (cdr (cdr f) x))))))
        ((eq? (car f) '^) '(* (* ,(car (cdr (cdr f))) (^ ,(car (cdr f)) (- ,(car (cdr (cdr f))) 1))) ,(deriv (car (cdr f)) x)))))

an error occurs

define: bad syntax (multiple expressions after identifier) in: (define deriv (f x) (cond ((eq? f x) 1) ((eq? (car f) (quote +)) (quote (+ (unquote (deriv (car (cdr f) x))) (unquote (deriv (car (cdr (cdr f) x))))))) ((eq? (car f) (quote -)) (quote (- (unquote (deriv (car (cdr f) x))) (unquote (deriv (car (cdr (cdr f) x))))))) ((eq? (car f) (quote *)) (quote (+ (* (unquote (car (cdr (cdr f)))) (unquote (deriv (car (cdr f) x)))) (* (unquote (car (cdr f))) (unquote (deriv (car (cdr (cdr f) x)))))))) ((eq? (car f) (quote ^)) (quote (* (* (unquote (car (cdr (cdr f)))) (^ (unquote (car (cdr f))) (- (unquote (car (cdr (cdr f)))) 1))) (unquote (deriv (car (cdr f)) x)))))))

p.s. Sorry for my English

5
  • what language (#lang) are you using? Commented Apr 22, 2018 at 18:24
  • use #lang racket Commented Apr 22, 2018 at 18:27
  • 1
    #lang racket has cadr, has it not? Commented Apr 22, 2018 at 18:28
  • Used, but I can't use them. These are the conditions of the job. Thanks :) Commented Apr 22, 2018 at 18:49
  • ah, OK, but it seems you do know how to deal with this: just use the chained applications of car and cdr according to the letters a and d: e.g., caddar -> (car (cdr (cdr (car . Commented Apr 22, 2018 at 18:59

1 Answer 1

1

You can use number? instead of numberp, and write

(define (func arg1 arg2 ...) ...)

in place of (defun func (arg arg2 ...) ...).

Also, don't be replacing the backquote ` with a simple quote '. Keep it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.