Square Root using Newton’s Method in scheme

Scheme is a new language for me. I came across Scheme from the book Structure and Interpretation of Computer Programs(SICP).

Scheme is a pure functional programming language. Scheme is a dialect of Lisp. My first program in scheme is the implentation of square root using Newton’s method.

Newton’s method of finding square root is based on the continuous refininig of guessed square root. We can almost avoid usage of loops in scheme by using the concept of recursion. In Newton’s method first we have to guess a square root, say 1. Then we must refine the guess by making the new guess value as average of guess and the number divided by guess.

guess = (guess + x / guess) / 2

The one important thing to remember in scheme is to make sure all the mathematical expressions given are in the prefix form.

Starting from the squareroot() method, we call the method sqrot with parameters 1.0 and x where 1.0 is the guess. In the sqrot method we check whether our guess is good enough, in this case if the difference between square of guess and x is less than or equal to 0.001 its good enough. If the guess in good enough the guess is returned else the guess is refined further using the average equation.

In the above code, everything is method. There is no need of explicit return statement. It will automatically return to the called method with a return value.

We can use the square root method by just running the command (squareroot 10) in the drscheme command line.


About Odol Shinu

I've completed my B Tech in Information Technology in 2010 from Government Engineering College Sreekrishnapuram Palakkad under Calicut University.

Posted on September 18, 2010, in scheme. Bookmark the permalink. 1 Comment.

  1. Hi,

    Quickie, since I don’t know if this message will even be read:

    I have a query about functional programming…

    I have been puzzling over the benefits. Knowing a bit of maths I came here since I felt pretty sure there’d an interesting solution to the problem using functions, trouble is… so what?

    Sometimes programming techniques are developed which to me seem like purely technical curiosities rather than heavyweight methods with incontrovertible advantages. Surely, you can solve all sorts of problems with recursion, but so what? Surely usually there isn’t any definite advantage of doing so except to make cool looking programs?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: