Exercise1-39 <---> Exercise1-41

Exercise 1.40

Define a procedure cubic that can be used together with the newtons-method procedure in expressions of the form

(newtons-method (cubic a b c) 1)

to approximate zeros of the cubic x3 + ax2 + bx + c.


Scheme solution:

(define (cubic a b c)
  (lambda (x) (+ (* x x x) (* a x x) (* b x) c)))

Haskell solution: (because of currying, we can just pretend x is just another argument)

cubic a b c x = x*x*x + a*x*x + b*x + c

Or you can write it out like Scheme above if you really like (there's no difference):

cubic a b c = \x -> x*x*x + a*x*x + b*x + c

OCaml solution: (because of currying, we can just pretend x is just another argument)

let cubic a b c x = x *. x *. x +. a *. x *. x +. b *. x +. c

Or you can write it out like Scheme above if you really like (there's no difference):

let cubic a b c = fun x -> x *. x *. x +. a *. x *. x +. b *. x +. c

Standard ML solution: (because of currying, we can just pretend x is just another argument)

fun cubic (a, b, c) x = x*x*x + a*x*x + b*x + c

Or you can write it out like Scheme above if you really like (there's no difference):

fun cubic (a, b, c) = fn x => x*x*x + a*x*x + b*x + c

Exercise1-39 <---> Exercise1-41


Comments


:) :)) :( ;) :\ |) X-( B) Markup

Exercise1-40 (last edited 2009-03-24 02:46:06 by FirstnameLastname)