We can define a function that represents the polynomial:
Code: Select all
(define (poly x)
(+ 5 (mul 7 x) (mul 3 (pow x 2))))
(poly 0)
;-> 5
Code: Select all
(for (x 0 10) (println x { } (poly x)))
;-> 0 5
;-> 1 15
;-> 2 31
;-> 3 53
;-> 4 81
;-> 5 115
;-> 6 155
;-> 7 201
;-> 8 253
;-> 9 311
;-> 10 375
For example, the polynomial:
Code: Select all
y(x) = 4*x^3 + 5*x^2 + 7*x + 10
Code: Select all
(lambda (x) (add 10 (mul x 7) (mul (pow x 2) 5) (mul (pow x 3) 4)))
Code: Select all
(define (make-poly coeff)
(local (fun body)
(reverse coeff)
(setq fun '(lambda (x) x)) ;funzione lambda base
(setq body '()) ;corpo della funzione
(push 'add body -1)
(push (first coeff) body -1) ;termine noto
(push (list 'mul 'x (coeff 1)) body -1) ;termine lineare
(for (i 2 (- (length coeff) 1))
(push (list 'mul (list 'pow 'x i) (coeff i)) body -1)
)
(setq (last fun) body) ;modifica corpo della funzione
fun
)
)
Code: Select all
(setq poly (make-poly '(4 5 7 10)))
;-> (lambda (x) (add 10 (mul x 7) (mul (pow x 2) 5) (mul (pow x 3) 4)))
Code: Select all
(poly 0)
;-> 10
Code: Select all
(for (x 0 10) (println x { } (poly x)))
;-> 0 10
;-> 1 26
;-> 2 76
;-> 3 184
;-> 4 374
;-> 5 670
;-> 6 1096
;-> 7 1676
;-> 8 2434
;-> 9 3394
;-> 10 4580