Reputation: 23
When I enter the following:
(define (root a b c)
(/ (+ (-b) (sqrt (- (exp b 2) (* 4 a c)))) (* 2 a)))
and then enter:
(root 3 6 2)
I get a message indicating that the procedure had two arguments but only requires exactly one. What am I doing wrong?
Upvotes: 2
Views: 755
Reputation: 236004
The procedure exp
raises the number e
to the power of its argument, if you need to raise an argument to the power of another argument, use expt
. Even better, given that you only need to square b
, a simple multiplication will do. Like this:
(define (root a b c)
(/ (+ (- b) (sqrt (- (* b b) (* 4 a c))))
(* 2 a)))
Upvotes: 1
Reputation: 723
The function the error refers to is exp
which takes only one argument. The exp
function calculates the exponential function, not an exponent. You want expt
, which raises a root x
to the exponent y
:
(expt b 2)
You can also just multiply the number times itself.
I usually keep R5RS or The Scheme Programming Language on hand since these basic functions can be hard to keep straight.
Upvotes: 1
Reputation: 176665
The exp
function doesn't do exponents really, it does something else mathy. (I don't know.)
What you want is usually called pow
for "power" but probably isn't defined in your environment, so I suggest you just define your own square
method:
(define (square x) (* x x))
And then:
(define (root a b c)
(/ (+ (- b) (sqrt (- (square b) (* 4 a c)))) (* 2 a)))
Edit: Oh, you'll also have to change a couple spacing issues, like (* 4 a c)
instead of (*4 a c)
, and (- b)
instead of (-b)
. You always have to separate the operator from the operands with spaces.
Upvotes: 4