0

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 '(* x y (+ 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.

Slater
  • 21
  • 1
  • 7
  • You currently have functions that work with fixed-size data. You want to design functions that deal with vararity data. See: http://www.ccs.neu.edu/home/matthias/HtDP2e/part_two.html for a treatment on designing functions that deal with lists of arbitrary size. – dyoo Dec 01 '12 at 05:29
  • Your bad syntax error is coming from the `if` statement. Since you're only allowed one form in its body, you'll want to wrap it up with a `begin`. E.g. `(begin (cons...) (cons...) (cons...))`. And don't give up on scheme yet! You can do some really awesome thing with LISP, but there definitely is a learning curve. – robbyphillips Dec 01 '12 at 15:05

1 Answers1

0

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 '(+ a b) 'b '(* a b)) will return '(+ a a b b (* a b)), 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/

m9__
  • 101
  • 5