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

SICP Exercise 1.3

Name: Anonymous 2008-01-19 7:59

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

(define (ex3 x y z)
                     (cond
                       ((> y z x) ((define a x) (define b y) (+ (square a) (square b))))
                       ((> y x z) ((define a x) (define b y) (+ (square a) (square b))))
                       ((> z y x) ((define a x) (define b y) (+ (square a) (square b))))
                       ((> z x y) ((define a x) (define b y) (+ (square a) (square b))))
                       ((> x z y) ((define a x) (define b y) (+ (square a) (square b))))
                       ((> x y z) ((define a x) (define b y) (+ (square a) (square b)))))


I know I'm thinking about this the wrong way. My solution doesn't work.

Name: Mr. Vile !l9/dToUs32 2010-10-05 12:11

    #!/usr/bin/perl
    @_ = sort @ARGV;
    print $_[1]**$_[1]+$_[2]**$_[2];

Name: Anonymous 2010-10-05 12:16

>>84
And people say Perl is ugly. That is the most beautiful solution in this thread.

Name: Anonymous 2010-10-05 13:09

>>84,85
It's also completely wrong.  It doesn't square the arguments (unless they happen to be 2) and it sorts them as strings instead of numbers.

Also, assigning to @_ considered harmful.

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