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:
Anonymous2009-09-04 19:30
which of course should be (define max
(λ(a . rest)
(let L ((max a)
(rest rest))
(if (empty? rest)
max
(L (if (> a (car rest)) a (car rest)) (cdr rest))))))
Fuck, I came up with this after reading the first sections of SICP (leading up to the exercise obviously):
(define (sum-of-high-two-sq a b c)
(cond ((< a b) (if (< a c)
(+ (square b) (square c))
(+ (square a) (square b))
))
(else (if (> b c)
(+ (square a) (square b))
(+ (square a) (square c))))))
As I look through the code posted in this thread I realize I still have far to go to become an EXPERT PROGRAMMER
Fucking faggotry. There's three solutions, nested conditional, sorting the sequence, or using min/max. this thread is over. anything else is posted by fucking faggots that shouldn't be on /prog/ anyawy and the only thing they do is pollute it with nonsense unrelate to programming and redundant crap.
>>58 the only thing they do is pollute it with nonsense unrelate to programming and redundant crap.
Wait a minute... It took you this long to figure that out?
>>63
No. This uses a tonne of extra clock cycles. If you had any clue how compilers work you would understand why this is going to take about 2-3 times as long as conditionals.
(define (square a) (* a a))
(define (sum-of-squares a b) (+ (square a) (square b)))
(define (func x y z)
(cond ((and (>= y x) (>= z x)) (sum-of-squares y z))
((and (>= x y) (>= z y)) (sum-of-squares x z))
((and (>= x z) (>= y z)) (sum-of-squares x y))))
74 posts and no recursive solution? (define (square x) (* x x))
(define (sum-of-squares x y) (+ (square x) (square y)))
(define (sum-of-largest-two-squares x y z)
(if (and (<= x y) (<= x z))
(sum-of-squares y z)
(sum-of-largest-two-squares y z x)))
Name:
Anonymous2010-10-05 9:16
lambda *n: sum(map(lambda x: x * x, sorted(n)[-2:]))
Name:
Anonymous2010-10-05 9:22
>>72
Know what I liked about QBasic? UPPERCASE KEYWORDS and relatively devoid of ant shit (;:.,). I loathe type characters though.
Name:
Anonymous2010-10-05 9:49
SLOW AS FUCK HIPSTER
#!/usr/bin/env ruby
def foo(a,b,c)
l=0
r=0
[a,b,c].each{|e|e<l&&l=e}.reject{|e|e==l}.map{|e|e*e}.each{|e|r+=e}
r
end
puts foo(10,3,5)
Name:
Anonymous2010-10-05 11:19
>>78
...and yet cleaner, easier to read and much more intelligible than all the other Lithpth code posted. Feature that.