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.
You used a Fold Stone on max.
What's that? max is evolving?
<<da da da da da da
dadadadadadadada
dada dada!>>
Congratulations! max now accepts arbitrary number of arguments!
Name:
Anonymous2008-01-19 14:30
This is very easy. You should've been able to figure this out yourself.
(define (square a) (* a a))
(define (sum-of-squares a b) (+ (square a) (square b)))
(define (ex3 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))))
It is simply a process of working out the smallest number, and then you know which two are the largest.
>>19
It's the best solution in this thread. That's not saying much, though.
>>20
REAL COMPUTER PROGRAMS ARE NOT WRITTEN IN LISP
Name:
Anonymous2008-01-19 16:31
>>21
ARE YOU STUPID OR SOMETHING. IT IS NOT A CHALLENGE TO DO SOMETHING USEFUL IN REAL LANGUAGES, IT IT HOWEVER TO DO IT IN RISPU. THANK YOU FOR YOUR ATTENTION.
(FUNNY FACT: I HAD NO IDEA I HAD THE CAPS LOCK ON WHEN WRITING >>20)
(FUNNY FACT: YOU JUST LOST THE GAME)
Name:
Anonymous2008-01-19 17:15
>>22
IT IT? >>21
No it's not, you are using a fucking sort algorithm.
Name:
Anonymous2008-01-19 19:30
<?php
function faggot(){$a=func_get_args();@array_pop(rsort($a));return(int)($a[0]*$a[0])+($a[1]*$a[1]);}
echo faggot(7,5,3)
?>
>>29
I do understand what >>4 posted. I used a variant of if a>b and b>c then a>c
where if a>b and b>c then a+b>c (where a and b are positive)
to solve it.
If (a > b)
x = a
if (b > c); y = b; else; y = c; end
else
x = b
if (a > c); y = a; else; y = c; end
end
needs three comparisons.
Name:
Anonymous2009-09-04 19:28
(define max
(λ(a . rest)
(let L ((max a)
(rest rest))
(if (empty? rest)
a
(L (if (> a (car rest)) a (car rest)) (cdr rest))))))
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.
// Defines a procedure that takes three numbers as arguments
// and returns the sum of the squares of the two larger numbers.
// @param a A number
// @param b A number
// @param c A number
// @return The sum of the squares of the two larger numbers
function eq3_1(a, b, c) {
var args = Array.prototype.slice.apply(arguments);
var n = Math.max.apply(null, args);
var m = Math.max.apply.(null, args.splice(args.indexOf(n)), 1);
return n*n + m*m;
}