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

Pages: 1-

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-14 22:14


(define (largest-two-of-three x y z)
   (if (>= x y) ; if x is greater than or equal to y...
       (sum-of-squares x (if (>= y z) y z)) ; then either y is larger, or z is larger
       (sum-of-squares y (if (>= x z) x z)))) ; otherwise x or z is larger

Name: Anonymous 2010-01-14 22:19

>>2
Thanks, I figured it out from simply re-reading it over and over until it made sense, it's an if statement that checks for which is larger, y or z, and if y is larger, y is the result, and if not, z, correct?

I guess it's just a learning process.

Name: Anonymous 2010-01-14 22:25

>>3
Technically, it's an if expression (something that resolves to a value), which is not quite the same as a statement (an action to take). Otherwise, yes, that's correct.

Name: Anonymous 2010-01-14 22:33

>>4
Thanks. saging because it's solved.

Name: Anonymous 2010-01-14 23:01

You could say it's a sort expanded using a human macroexpander.
In a general case:given an arbitrary number of integers, return the sum of squares of the first n numbers, you would just use a sort function to sort the entire list/array/sequence and then just calculate the sum of squares of that part of the list. Here's a general, but slower solution I just wrote on 2 minutes:


(defun n-biggest-numbers (sequence n)
  (subseq (sort sequence #'>) 0 n))

(defun square (x) (* x x))
(defun sum-of-squares (numbers)
  (apply #'+ (map 'list #'square numbers)))

(defun sum-of-squares-of-n-biggest-numbers (sequence &optional (n 2))
  (sum-of-squares (n-biggest-numbers sequence n)))

Excuse my CL, but I don't have any Scheme implementations installed on this box.

Name: sage 2010-01-14 23:09

Erm, it should have been (sort (copy-seq sequence) #'>)...

Name: Anonymous 2010-01-15 0:56

>>6
You don't have either "sort" or "map" in Chapter 1 of SICP. That's the point.

And, you're right CL is ugly

Name: Anonymous 2010-01-15 0:58

>>8
It's easy enough to implemented them. I think everyone writes their own maps and sorts anyway when learning.

As for your other comment: Beauty is in the eye of the beholder.

Name: >>9 2010-01-15 1:06

sure is hard to implement mapcar:

(defun simplistic-mapcar (list fun)
  (if (null list)
      nil
      (cons (funcall fun (car list)) (simplistic-mapcar (cdr list) fun))))

As for qsort or equivalent, it can be done in less than a dozen lines.

However, even if that would work for this simple problem, the real mapcar and sort functions are a lot more featureful (and harder to implement), however those features are not needed for this problem.

Name: Anonymous 2010-01-15 1:32

>>9
>>10
And why do you think it's better to implement "map" here? Why didn't you implement "sort"? How about sequences? Do you REALLY think all that crap is totally necessary for solving this simple exercise?

Ugliness is the design pillar of CL.

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!

Name: >>12 2010-01-15 2:02

Implementing general sequences is of course a lot more work. It's not that hard to do if you have a clear API defined(the one specified in the Hyperspec and the SBCL extensions are what I'd go for), but nonetheless it's likely to be a non-trivial amount of work. One can implement a much smaller subset for solving this problem if they wanted, but in my case(and in other real-world cases), these things come with the implementation.

Name: Anonymous 2010-01-15 2:05


I'll say this again: Beauty is in the eye of the beholder..
I find PHP 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 Java 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!

Name: Anonymous 2010-01-15 2:09

>>14
IHBT.

Name: Anonymous 2010-01-15 2:16

>>12
why would anyone want to reinvent the wheel more than a few times (for learning purposes, or maybe they're writing their own implementation)?
Did you even read the fucking OP????

Name: Anonymous 2010-01-15 2:29

>>16
I was not answer to the OP(>>1), I was answering to >>11. Sorry about that typo.

Name: Anonymous 2010-01-15 4:31

but don't go claiming Scheme doesn't have some of them too
*cough*values*cough*

Also, recursive solution
(define (largest-two-of-three x y z)
  (if (<= x y z)
      (sum-of-squares y z)
      (largest-two-of-three y z x)))

Name: Anonymous 2010-01-15 4:46

an extensible mudball with a large variety of features is what most of them want!
And yet Lisp is underused.

Name: Anonymous 2010-01-15 6:10

>>19
Language choice tends to largely depend on marketing and sometimes  support for the latest buzzwords. Few users make conscious choice when it comes to programming languages.

Name: Anonymous 2010-01-15 12:39

>>18
I like it. But (<= 4 5 4) is #f, which is irritating. (Not that the recursion won't find the right answer anyway.)

Name: Anonymous 2010-01-15 13:11

>>21
Why does it irritate you? The standard requires that <= be monotonically non-decreasing or at least r5rs did i.e. (<= 1 2 3 4 ...) is the same as 1 <= 2 <= 3 <= 4 <= ...
I don't think it would be better if it acted like
(define (<=* first . rest)
    (any (λ (x) (<= first x)) rest))

Name: Anonymous 2010-01-15 15:04

>>12
English is not my native but even I can understand what is written.
Scheme have both sort and map for "general solution", but if you need to solve some exercise with only several basic primitives and you come up with "general solution" - you just DID NOT meet the requirements.

>>18
Awesome

Name: Anonymous 2010-01-15 15:14

>>22
Why does it irritate you?
The question it seems to be asking is, "Is the first element less than or equal to the other elements?" That is, it seems to me to be asking
(andmap <= (list x y z))
which would be true. I'm fine with it being false, it just isn't what I would intuit.
non-decreasing
It's a useful interpretation and I really like it, as it means "is b between a and c?" is just
(<= a b c)
and that's great.

Name: Anonymous 2010-01-15 15:15

>>24
That andmap is not even matching my description, please ignore it... :(

Name: Anonymous 2010-01-15 16:01

>>24
The question it seems to be asking is, "Is the first element less than or equal to the other elements?"
In that case, would it not make more sense for the second argument to be a list? Say <=all?[1] : number listof(number) -> boolean
You could then have the functions <all? >all? and >=all? (arguably =all? could also be an identifier that shadows =)

--
1. Scheme identifiers would usually be named with the <=? at the end, but in this case I think that all<=? would imply the contract listof(number) number -> boolean

Name: Anonymous 2010-01-15 17:27

In that case, would it not make more sense for the second argument to be a list?
Not really. It's like min or +.

[code](define <=?
  (λ vals
    (let ((lhs (first vals)))
      (andmap (λ(x) (<= lhs x)) (rest vals)))))
(<= 4 5 4) ; -> #f
(<=? 4 5 4) ; -> #t

Name: Anonymous 2010-01-15 18:44

>>27
I see where you are coming from, but I respectfully disagree.

Name: Anonymous 2010-01-15 19:03

>>28
No, no, like I said, I have no problem with the interpretation, I think it is useful. It just wasn't what I expected. But this discussion has shown me that my interpretation was just a simple restatement of
(lambda (x . args) (= x (apply min args)))
which is really kind of dumb, now that it has been stated plainly.

Name: Anonymous 2010-01-16 3:53

Sorry I'm late. newLISP

(define (largest-two-of-three x y z) (apply + (clean (curry = (min x y z)) (list x y z))))

Name: ​​​​​​​​​​ 2010-10-23 10:01

Name: Anonymous 2011-01-31 19:56

<-- check em dubz

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