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: Anonymous 2008-01-19 8:12

Read SICP

Name: Anonymous 2008-01-19 8:13

Name: Anonymous 2008-01-19 8:45

Simple shit
`if a > b and b > c, then a > c'
^^ VERY BASIC STUFF.


(define (ex3 x y z)
    (+ (square (max x y)) (square (max (min x y) z))))

Name: Anonymous 2008-01-19 10:07

(define (ex3 x y z)
   (- (+ (square x) (square y) (square z)) (square (min x y z))))


Name: Anonymous 2008-01-19 10:17

needs moar (+ (square a) (* 2 a b) (square b))

Name: Anonymous 2008-01-19 10:32

>>6
(a+b)^2? why? fail.

Name: Anonymous 2008-01-19 13:44

I don't know LISP, but if i wanted to break it down functionally, i'd break it down this way:

First have a function that takes the three numbers and returns the two largest.

Second, have a function that squares two numbers, adds them and returns the result.

The argument of the second function is the result of the of the first function etc.





The design of the first function should be trivial. Finding the two largest numbers is the hard part of the problem.

I'm too drunk too push around the variables right now so I'll lets some other adequately quallified computer scientist fag do it

Name: Anonymous 2008-01-19 13:49

>>8
thank you for your valuable contribution!

Name: Anonymous 2008-01-19 13:54

>>8
The question is;

Give a method with the lowest number of comparisons for finding the two greatest numbers out of a set of three numbers.

to all the code monkeys on /prog/, this is the pure computer science shit you will never, ever know.

Name: Anonymous 2008-01-19 14:00

Use foldr to evolve max.

Name: Anonymous 2008-01-19 14:05

>>4
>>5
SICP doesn't even mention min and max at that point.

>>11
or foldr.

Name: Anonymous 2008-01-19 14:08

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: Anonymous 2008-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.

Name: Anonymous 2008-01-19 14:34

>>12
(define (min a b)
       (if (< a b) a b))

(define (max a b)
       (if (> a b) a b))


Surely you should be smart enough to define it yourself.

Name: Anonymous 2008-01-19 14:36

>>14
You could probably add an else case as

(else (sum-of-squares x y))

for when all the values are the same. It would depend on whether the problem states that there will always be two larger values or not.

Name: Anonymous 2008-01-19 14:46

>>4
Best solution.

Name: Anonymous 2008-01-19 15:57

: ex3 ( x y z -- r ) 3array natural-sort [ sq ] each + nip ;

Name: Anonymous 2008-01-19 16:03

>>18
Terrible solution.

Name: Anonymous 2008-01-19 16:12

>>18
NOT LISP

Name: Anonymous 2008-01-19 16:15

>>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: Anonymous 2008-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: Anonymous 2008-01-19 17:15

>>22
IT IT?
>>21
No it's not, you are using a fucking sort algorithm.

Name: Anonymous 2008-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)
?>

Name: Anonymous 2008-01-19 21:57

sicp a b c = sum $ map (^2) $ tail $ List.sort [a,b,c]
lol readable

Name: Anonymous 2008-01-20 6:12

>>25
FAILTROLL

Name: Anonymous 2008-01-20 21:43

OP here, figured out to find the smallest number next time I sat down. Thanks for all the examples though.

I got this:

(define (ex3 x y z)
  (cond ((< x (+ y z)) (sum-of-squares y z))
    ((< y (+ z x)) (sum-of-squares z x))
    ((< z (+ x y)) (sum-of-squares x y))))

Name: Anonymous 2008-01-21 14:14

>>27
(ex3 3 2 2)

Name: Anonymous 2008-01-21 14:32

>>27
Will you faggot try to understand what >>4 posted?
Please?

Name: Anonymous 2008-01-21 15:15

>>25
lol readable
sicp a b c = case List.sort [a, b, c] of [_, x, y] -> x ^ 2 + y ^ 2

Name: Anonymous 2008-01-21 20:14

>>28
THIS INPUT DOES NOT HAVE 3 LARGEST NUMBERS

Name: OP 2008-01-21 20:31

>>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.

Name: Anonymous 2009-03-18 3:41

I wants lots and lots of some delectable pot!

Marijuana MUST be legalized.

Name: Anonymous 2009-09-04 17:40

>>4

max is not allowed.

Name: Anonymous 2009-09-04 17:57

def ex3(a,b,c); [a,b].max**2+[[a,b].min,c].max**2; end

TOO EASY

Name: Anonymous 2009-09-04 17:58

>>35

also, if you need to use any kind of sort to solve this exercise you are fucking pathetic. go back to /b/ immediately.

Name: Haxus the SICP-reader 2009-09-04 17:58

I'm actually working on this right now myself.  I must shield my eyes from this thread until I solve it on my own!

Name: Sagey McSagerson !!WGlXfZ0twvi7ADs 2009-09-04 18:03

For this act of Thread Necromancy >>33-kun is awarded the rank of Lesser Thread Necromancer.


32  Name: OP : 2008-01-21 20:31
[omitted for brevity]
33 Name: Anonymous : 2009-03-18 03:41

Name: Anonymous 2009-09-04 18:04

>>10

Input: a, b, c
Output: x, y

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: Anonymous 2009-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))))))

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