I wanna learn programming. (40)

18 Name: #!/usr/bin/anonymous : 2008-02-10 18:54 ID:Heaven

>>17
Math? Oh come on.
Consider this simple problem from SICP.
More than half of the programmers I have met cannot think of the1 solution in 1 minute. It might take more time, or they may never come up with the solution.

Here is the problem:

``Write a procedure that takes 3 arguments and returns the sum of the squares of the two larger values''

Ignoring the squares and the sum part, this problem essentially asks you to find the two higher values in 3 values and do something with them.
That is ridiculously easy. I insist that if someone cannot work it out, programming is not for him.

Before you read further, try to solve it.

The solution is simple: Use min and max.
(max x y) will tell you which is larger, x or y.
You can safely assume that is one of the two larger values.
What remains, is z and the other value.
The other value, we can determine with min.
(min x y) will tell you which is smaller from these two.
Then, we again have two values and need the higher one.
We will use max again
(max (min x y) z) will do the trick. Simple.


________________________________________________________

1 There are many solutions, some efficient, some not. Some dumb, some clever. I am refering to the smart and efficient solution as ``the'' solution.
This thread has been closed. You cannot post in this thread any longer.