everybody! So, I am a new student in a computer science class. I really have only been in there for a few weeks so I don't know much but my teacher really threw me a curveball. Just when I thought I was getting the hang of things he tells us to create a function in scheme that takes two values and then outputs the "maximum" value of the 2 or the largest. The twist is that we can only use the operators that we've learned so far... which is addition, subtraction, division, multiplication, and abs (like absolute value). I'm totally stumped and I was wondering if someone could help me out...
-
Please post the code you've written so far, pointing out the difficulties – Óscar López Sep 21 '16 at 22:03
1 Answers
The answer should be irrelevant of the language you implement it in.
This is the formula you need to implement:
a-(((a-b)-abs(a-b))/2)
Proof:
let {a,b} = {8,10}; then:
8-(((8-10)-abs(8-10))/2) = 8-((-2-(2))/2) = 8-(-4/2) = 10
let {a,b} = {10,8}; then:
10-(((10-8)-abs(10-8))/2) = 10-(((2-(2)/2) = 10-(0/2) = 10
Explanation:
As you can see, the answer is based on calculating a suitable correction and subtracting it from the first number. The correction is calculated in such a way that if the first number is greater, then the correction is zero, while if the second number is greater, then the correction is the difference between the first and the second number, so subtracting it from the first yields the second.
To calculate the correction, we first take the difference between the two numbers, and we subtract from it the absolute of the difference. There are two possibilities:
If the first number is greater, then the difference will be positive, so the subtraction will yield a correction of zero, which is exactly what we want.
If the second number is greater, then the difference will be negative, so subtracting its absolute will double it. We then divide by two in order to get the right correction. The cool thing about division is that it does not affect the first possibility, because dividing zero by two still yields zero.
Implementation:
The task of implementing the above calculations in scheme is left as an exercise to the student.

- 56,297
- 11
- 110
- 142
-
If your teacher asks you to implement this for a list of three numbers, *then* you will be in trouble. – Mike Nakis Sep 21 '16 at 22:13
-
Thank you so much! This is a great help and I was wondering if you could share your thought process so that I can better understand and maybe get it on my own next time? I was guessing and checking various random functions so obviously that wasn't a great method... and I just wanted to know yours. – May Sep 21 '16 at 23:08
-
My thought process begun with the idea that `abs()` must play a major role, because in order to sort two numbers you kinda need an `if`, and from the available operations `abs()` is the only one that has a built-in kind of `if`. Then I picked two numbers, 8 and 10, brainstormed for several minutes with various different ways that these two numbers can be included in an expression with `abs()`, and I came up with what you see in the explanation section in the answer. – Mike Nakis Sep 22 '16 at 05:57
-
As you can see, it took me about 10 minutes, and when I started thinking about it I was not sure at all that I would manage to come up with an answer. I am quite proud of myself. Don't expect to do so well without a few years of experience, but then again be certain that with enough experience on your back you can do it too. – Mike Nakis Sep 22 '16 at 06:01