Page 1 of 1

Problem Solving With Limited Resources

Posted: Thu Jan 01, 2015 11:31 am
by Datagrace
I am a non-programmer who is learning Scheme in order to better understand JS and the history of programming. I'm using a college textbook from the '90's, 'Exploring Computer Science With Scheme, and working through an exercise (p 52) that asks: Write a function that takes five numbers and returns the average of the middle three (dropping the highest and lowest values).

This would normally not be a very challenging exercise in any environment where I am comfortable, but it's very early in the book. We have no logical branching yet, and no loop structure. Sub-routines have been hinted at, but not yet explained, and let() is the next section. All we have learned so far is mathematical operators (so we do have min and max), and function and variable definition.

Although later chapters provide exercise answers, there are none for this chapter. Any suggestions for how to proceed, but without loops, let(), or if() statements?

Thank you,

John Weinshel

Re: Problem Solving With Limited Resources

Posted: Mon Jan 05, 2015 7:26 am
by Goheeca
And can you use sort? :D

If not, than there is also a way. You can find out the minimum and the maximum, sum all up and subtract those extremes and divide by 3. No need for defining of variables.

The solution:

Code: Select all

(define (f a b c d e)
    (/ (-   (+ a b c d e)
            (min a b c d e)
            (max a b c d e))
        3))
and its online preview.

Re: Problem Solving With Limited Resources

Posted: Mon Jan 05, 2015 10:15 am
by Datagrace
Oh, of course. Thank you.

Yes, no sort yet.

I clearly have to learn to think different, more simply.

Re: Problem Solving With Limited Resources

Posted: Mon Jan 05, 2015 1:35 pm
by Goheeca
Yeah, one must change the approach (in functional programming), be more declarative than imperative. Right now, I'm learning Haskell (deeper and deeper) and the pure functional programming has its own glamour.