Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

I feel retarded, /prog/

Name: Anonymous 2010-01-14 22:05

I'm on SICP, and I've never programmed before, and I'm stuck on the most basic of SICP exercises...

Exercise 1.3.  Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

Being the weakling that I am, I turned to google for help, and returned this program:

(define (square x) (* x x))
 
 (define (sum-of-squares x y)
   (+ (square x) (square y)))
 
 (define (largest-two-of-three x y z)
   (if (>= x y)
       (sum-of-squares x (if (>= y z) y z))
       (sum-of-squares y (if (>= x z) x z))))


It's the largest part I'm having problems with:
It defines the function "largest-two-of-three" with variables 'x', 'y' and 'z'. It goes on to state the parameters of the function as, if x is greater than y, do the sum of squares for x and, if y is greater than or equal to z, y and z, and if y isn't larger or equal to z, then z? Or, if x is less than y, do the sum of squares for y and, if x is greater than or equal to z, x, if not, z?

I know this is a really sad place to be stuck on, but I want to learn how to program rather much, and I won't let this stop me

Name: Anonymous 2010-01-15 1:56

>>1
And why do you think it's better to implement "map" here?
I showed how the problem could be solved in a general manner. While it's perfectly possible to just implement this in a general way using only if, arithimethic operations, cons/car/cdr (assuming you're using lists, not arrays/sequences), apply, null,... why would anyone want to reinvent the wheel more than a few times (for learning purposes, or maybe they're writing their own implementation)?
Why didn't you implement "sort"? How about sequences?
Doing a naive sort is very simple, a qsort is only slightly harder, however my original code works with both arrays and lists, and in some implementations it can work even on user-defined sequences, this makes it very general!
Do you REALLY think all that crap is totally necessary for solving this simple exercise?
No, but I think a general solution which clearly shows the intent of the code is much more beautiful than manually doing comparisons, which is why I posted it.
Ugliness is the design pillar of CL.
I'll say this again: Beauty is in the eye of the beholder..
I find CL to be quite well designed myself. It allows one to make very general solutions, it took into account many possible problems programmers may encounter, it has a rich and mostly well-thought out library and many data types as well as giving the programmer many ways to extend the language to their own desire. It does have a few rough edges mostly due to legacy reasons, but don't go claiming Scheme doesn't have some of them too! However, such issues are only a problem for newbies: once someone learns of them, they're not a problem. Other languages have such historical idiosyncrasies and you don't see people complaining about them that much. (Natural languages moreso.)

Some people like having a tiny core language from which they can build anything. That is nice for learning, but people doing real development don't like wasting their time reinventing the wheel: They want a good tool which can be useful in a variety of situations, they don't want a beautiful pearl which can only be used in one way, an extensible mudball with a large variety of features is what most of them want!

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List