# Finding the cube root in Scheme

Page 1 of 1

## 3 Replies - 3303 Views - Last Post: 31 August 2012 - 03:28 AM

### #1 rarment

Reputation: 0
• Posts: 8
• Joined: 27-July 12

# Finding the cube root in Scheme

Posted 30 August 2012 - 07:58 PM

I'm working on homework for a class, and I'm having a really hard time with scheme. I just need to find an approximation of the cube root of a number,n, within a margin of error, p. I'm not sure how to implement the improve function and how to make all of these functions work together.

This is what I have so far, but I'm not sure if I am even on the right path. Some expert advice would be greatly appreciated!

```
(define (cube-root n p)
(if (good-enough? n p)
n
(cube-root (improve n ?) p))
)

(define (improve x y)
(/
(+
(/ x (expt y 2))
(* (2 y)))
3)

(define (good-enough? n p)
(< (abs (- (cube x) n) p))

)
```

Is This A Good Question/Topic? 0

## Replies To: Finding the cube root in Scheme

### #2 sepp2k

• D.I.C Lover

Reputation: 2242
• Posts: 3,440
• Joined: 21-June 11

## Re: Finding the cube root in Scheme

Posted 31 August 2012 - 02:10 AM

From your code it looks like you're trying to implement Newton's method and you don't know which value to use as the second argument to the improve function - is this correct¹? If so, do you know what the second value should be logically and you just don't know how to keep track of that value in the code or don't you fully understand Newton's method?

In general you should think about what each variable in your code is supposed to represent and then make sure that you use that variable in that way. For example the n in cube-root is supposed to be the number whose cube root you're supposed to find, correct? And the value returned by improve is supposed to be an improved guess of what the cube root might be, right? And you're then passing that possible cube root as the new value of n - does that make sense?

Also your good-enough? function takes 2 arguments, but uses 3.

¹ In the future it would help if you point these things out explicitly, so I don't have to guess based on the code.

### #3 rarment

Reputation: 0
• Posts: 8
• Joined: 27-July 12

## Re: Finding the cube root in Scheme

Posted 31 August 2012 - 03:17 AM

sepp2k, on 31 August 2012 - 02:10 AM, said:

From your code it looks like you're trying to implement Newton's method and you don't know which value to use as the second argument to the improve function - is this correct¹? If so, do you know what the second value should be logically and you just don't know how to keep track of that value in the code or don't you fully understand Newton's method?

Yes, I am trying to implement Newton's Method and I don't know what the second value should be and don't know how to keep track of the value.

Quote

In general you should think about what each variable in your code is supposed to represent and then make sure that you use that variable in that way. For example the n in cube-root is supposed to be the number whose cube root you're supposed to find, correct? And the value returned by improve is supposed to be an improved guess of what the cube root might be, right? And you're then passing that possible cube root as the new value of n - does that make sense?

Yes, that all is correct and makes sense

Quote

Also your good-enough? function takes 2 arguments, but uses 3.

I see that now. I was getting so frustrated and I guess I added a variable and just didn't fully understand what I was doing. I really am having a hard time with functional programming.

Quote

¹ In the future it would help if you point these things out explicitly, so I don't have to guess based on the code.

I will be more specific in the future, thank you!

### #4 sepp2k

• D.I.C Lover

Reputation: 2242
• Posts: 3,440
• Joined: 21-June 11

## Re: Finding the cube root in Scheme

Posted 31 August 2012 - 03:28 AM

rarment, on 31 August 2012 - 12:17 PM, said:

Yes, I am trying to implement Newton's Method and I don't know what the second value should be and don't know how to keep track of the value.

Do you mean you don't what the value is supposed to be in the code - or you don't know what it's supposed to be logically? If it's the latter, I think you should work on understanding Newton's method first before you try to implement it.

Quote

In general you should think about what each variable in your code is supposed to represent and then make sure that you use that variable in that way. For example the n in cube-root is supposed to be the number whose cube root you're supposed to find, correct? And the value returned by improve is supposed to be an improved guess of what the cube root might be, right? And you're then passing that possible cube root as the new value of n - does that make sense?

Yes, that all is correct and makes sense[/quote]

My point was, that it doesn't make sense. You're not supposed to calculate the cube root of the possible cube root - that's not how Newton's method works.

Quote

I really am having a hard time with functional programming.

If functional programming is the problem, try to implement it as imperative pseudo code first. That should at least give you a clearer idea of what each variable is supposed to be.

This post has been edited by sepp2k: 31 August 2012 - 03:29 AM