> On Nov 6, 2015, at 9:07 PM, dyrueta <[email protected]> wrote:
> 
> Hi All --
> 
> I'm hoping the answer to the question below will serve as an intro to macros, 
> a subject I've been interested in for awhile but haven't had time to look 
> into much.  Here goes, fingers crossed:
> 
> Given a list of constants (a0, a1… an) and a list of x-values (x0, x1,….xn), 
> I want to design a function in Racket that produces an interpolating 
> polynomial in Newton form, or p(x) = a0 + a1(x - x0) + a2(x - x0)(x - x1) + 
> a3(x - x0)(x - x1) (x - x2) + ….+ an(x - x0)(x - x1)…(x - xn).   My 
> understanding of functions that produce functions is limited to what’s in 
> HtDP, and this example (I think) goes beyond that (and hopefully into 
> macros).   
> 
> Any tips or advice would be much appreciated!


Let’s assume I don’t know what you’re talking about. But we do know together 
that HtDP requests examples for problems that you’re not quite familiar with. 
So we work through 3 instances of this problem: 

#lang racket

(require plot)

(define ((make-p-0 a0 a1) x0 x1)
  (define (p x)
    (+ a0
       (* a1 (- x x0))))
  p)

[plot (function ((make-p-0 1 2) 3 4)) #:x-min -10 #:x-max +10]

(define ((make-p-1 a0 a1 a2) x0 x1 x2)
  (define (p x)
    (+ a0
       (* a1 (- x x0))
       (* a2 (- x x0) (- x x1))))
  p)

[plot (function ((make-p-1 1 2 3) 3 4 5)) #:x-min -10 #:x-max +10]

(define ((make-p-2 a0 a1 a2 a3) x0 x1 x2 x3)
  (define (p x)
    (+ a0
       (* a1 (- x x0))
       (* a2 (- x x0) (- x x1))
       (* a3 (- x x0) (- x x1) (- x x2))))
  p)

[plot (function ((make-p-2 1 2 3 4) 3 4 5 6)) #:x-min -10 #:x-max +10]

I threw in the plots for fun. Now I think one of two things is wrong: 

 — your description (making the a-sequence as long as the x-sequence) 
 — or my understanding of your description. 

Correct me. 

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to