Problem Solving With Limited Resources

Discussion of Scheme and Racket

Problem Solving With Limited Resources

Postby Datagrace » Thu Jan 01, 2015 11:31 am

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
Datagrace
 
Posts: 2
Joined: Thu Jan 01, 2015 11:13 am

Re: Problem Solving With Limited Resources

Postby Goheeca » Mon Jan 05, 2015 7:26 am

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.
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.
User avatar
Goheeca
 
Posts: 271
Joined: Thu May 10, 2012 12:54 pm

Re: Problem Solving With Limited Resources

Postby Datagrace » Mon Jan 05, 2015 10:15 am

Oh, of course. Thank you.

Yes, no sort yet.

I clearly have to learn to think different, more simply.
Datagrace
 
Posts: 2
Joined: Thu Jan 01, 2015 11:13 am

Re: Problem Solving With Limited Resources

Postby Goheeca » Mon Jan 05, 2015 1:35 pm

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.
User avatar
Goheeca
 
Posts: 271
Joined: Thu May 10, 2012 12:54 pm


Return to Scheme

Who is online

Users browsing this forum: No registered users and 5 guests

cron