简体   繁体   中英

Improving a derivatives example in Scheme

I am trying to make a scheme derivatives calculator more accepting of inputs, starting with the sum procedure and eventually the product. I have been trying to modify the procedure to accept inputs in the form of (deriv '(* xy (+ x 3)) 'x) instead of (deriv '(* x (* y (+ x 3))) 'x) .

The code I am working off of is:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SUM RELATED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;makes a sum structure
(define (make-sum a1 a2)
    (list '+ a1 a2))


;checks if something is a sum structure
(define (sum? x)
(and (pair? x) 
   (eq? (car x) '+)))

;get first term of sum
(define (addend s) 
(cadr s))

;get second term of sum
(define (augend s)
(caddr s))

and

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;PRODUCT RELATED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;make a product structure
(define (make-product m1 m2) 
(list '* m1 m2))

;checks if something is a product structure
(define (product? x)
(and (pair? x) 
   (eq? (car x) '*)))

;get first factor of product
(define (multiplier p) 
(cadr p))

;get second factor of product
(define (multiplicand p) 
(caddr p))

I have attempted to solve the problem but without much luck so far, this is what I have tried to implement in several different variations:

;makes a sum structure
(define (make-sum a1 a2)
(if (sum? a1)
  (if (sum? a2)
      (cons '+ (append (cdr a1) (cdr a2)))
      (cons '+ (append (cdr a1) (list a2)))
      (cons '+ (append (list a1) (cdr a2)))))
    (list '+ a1 a2))

It is giving me a bad syntax call. And I can see that my second if really doesn't seem to fit an if statement, but the structure of how it is suppose to look was given to me by my teacher so it confuses me that it doesn't work, or is suppose to work I guess.

If anyone can give me a hand understanding this, it would be great.

I am not really enthused with Scheme so far, and while it seems like it could be cool to know or understand, my teacher is trying to fit it in, in the last 2 weeks and I cant get a grasp on it.

So, in your case you are having if-clause with 3 arguments, while it takes 2, so you get an error. If you want to fix your version, you shall use such version

(define (make-sum a1 a2)
(if (sum? a1)
  (if (sum? a2)
      (cons '+ (append (cdr a1) (cdr a2)))
      (cons '+ (append (cdr a1) (list a2))))
  (if (sum? a2)
      (cons '+ (append (list a1) (cdr a2)))
      (list '+ a1 a2))))

Alternatively, if you want more general code, you shall definitely use functions, working with variable number of arguments - use syntax (define (func-name . list-of-arguments) ...) . For example, you can have such make-sum function:

(define (make-sum . args)
  (cons '+ (apply append (map (lambda (x) (if (sum? x) (cdr x) (list x))) args))))

So, (make-sum 'a '(+ ab) 'b '(* ab)) will return '(+ aabb (* ab)) , guess that's what you expected.

Btw, didn't you got that program in sicp? If no, you shall definitely try it, it's a great book to learn sheme and programming in general. http://mitpress.mit.edu/sicp/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM