This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Problem Solving With Limited Resources

4 posts · 6388 views

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

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:
(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.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Problem Solving With Limited Resources

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

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.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.