Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:39:25+00:00 2026-06-13T04:39:25+00:00

I am writing a small program to compute simple derivatives of expressions represented in

  • 0

I am writing a small program to compute simple derivatives of expressions represented in list form i.e 2x^2 is represented as (* 2 (exp x 2)) and I have defined the following functions:

;; derivative of a constant is 0
(define (diff-constant x E) 0)

;; computes derivative of E with respect to x where E can only be of the form
;; (+ x x ...)
(define (diff-sum x E)
  (cond ((or (not (list? E)) (null? E)) 0)
        ((eq? (car E) x) (+ 1 (diff-sum x (cdr E))))
        (else (diff-sum x (cdr E)))))

;; computes derivative of E with respect to x where E can only be of the form
;; (* x y)
(define (diff-product x E)
  (cond ((and (eq? x (cadr E)) (eq? x (caddr E))) (list '* 2 x))
        ((eq? x (cadr E)) (list (caddr E)))
        ((eq? x (caddr E)) (list (cadr E)))
        (else 0)))

;; computes derivative of E with respect to x where E can only be of the form
;; (expt x y) which is x^y
(define (diff-expt x E)
  (cond ( not (eq? x (cadr E)) 0)
        ((eq? 1 (caddr E)) 0)
        ((eq? 2 (caddr E)) (cadr E))
        (else (list '* (caddr E) (list 'expt x (- (caddr E) 1))))))

I also have a dispatch table defined as:

;; Dispatch Table of supported operators.
 (define diff-dispatch
   (list (list '+ diff-sum)
         (list '* diff-product)
         (list 'expt diff-expt)
         ))

and I am trying to write a function diff that takes an equation E (in list form) and computes the derivative with respect to x and uses the dispatch table to call the pre-defined functions returning the result

here is what I have so far but I can’t figure out the rest

;; Differentiate expression E with respect to x.
(define (diff x E)
  (cond ((number? E) (diff-constant x E))
        ;; insert code here to handle the other base case - variables
        ...
        (else    ; insert code here to lookup the appropriate function in the
                 ; dispatch table based on the operator in the expression,
                 ; then call that function to differentiate the expression
                     (diff-func x E))))))

ex: (diff 'x '(+ x (* x x))) should evaluate to (+ 1 (+ (* 1 (* x)) (* x 1))) (i.e. 1 + x + x)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:39:26+00:00Added an answer on June 13, 2026 at 4:39 am

    In the SICP book there’s a whole section explaining in detail how to build a Scheme program for performing symbolic differentiation, take a look at section §2.3. In particular, be aware that you’re missing one case – what happens if the expression to be derived is a variable? check the link to make sure that you’re on the right track.

    Now, answering the question: it’s simple to implement a dispatcher given the table representation used in the code. Something along these lines will work for obtaining an applying the correct differentiation procedure:

    ((cadr             ; obtain the differentiation procedure
      (assoc           ; look for the differentiation procedure
       (car E)         ; obtain the operator in the expression
       diff-dispatch)) ; dispatch table
     x E)              ; apply the procedure with parameters `x` and `E`
    

    Notice that the “trick” for finding the correct procedure to apply lies in the fact that the table is implemented as an association list, and the assoc procedure was designed precisely for finding data in such a list. Read more about it in the documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with Regular Expressions. I'm writing a small program that matches
I'm writing a small program to record reading progress, the data models are simple:
I am writing a small program in C++ using OpenCV-2.3 API. I have an
I'm just a beginner in C++. I'm writing small and simple program that prints
I am writing a small program in OpenGL on my Mac. I have a
I'm writing a small program that prints a menu screen with a list of
I am writing a numpy/cython program to compute the minors of small matrices (a
I am writing a small program that sends and receive multicast packets.I need to
I am writing a small program for our local high school (pro bono). The
I am writing a small program that gets activated when sms is received.We need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.