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

Name: Haxus the Novice 2009-09-05 5:55

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

Name: Anonymous 2009-09-05 6:20

>>42
That's not a solution.

Name: Haxus the Failed 2009-09-05 6:27

>>43
Then I have even farther to go than I thought!

Nevertheless, I read >>4-chan's solution since I created my failed attempt so I'll just have to try harder on the next exercises.

Name: Anonymous 2009-09-05 6:45

Name: Anonymous 2009-09-05 7:29

>>45
weaboo faggot

Name: Anonymous 2009-09-05 7:32

>>46
Welcome to 4chan. Please, enjoy your stay.

http://www.4chan.org/japanese/

Name: Anonymous 2009-09-05 9:06

>>46
oh, wow.
you do realise that /prog/'s sister board is /jp/ right?

Name: bitches and hoes 2009-09-05 9:21

bitches and hoes

Name: Anonymous 2009-09-05 9:56

>>48
This gives a whole new meaning to ``Hax my anus, onee-chan!''. ( ≖‿≖)

Name: Anonymous 2009-09-05 10:02

>>50
Goddammit, now I'm imagining an older sister /jp/ totally raping /prog/ and I'm enjoying it ;)

Name: Anonymous 2009-09-05 10:05

(define (sum-largest-two x y z)
 
(sum (largest-two (list x y z))))

Name: Anonymous 2009-09-05 10:07

(define (sum-largest-two x y z)
 
(sum (map square (largest-two (list x y z)))))

Name: Anonymous 2009-09-05 10:28

i was going to reply with:


(define (sum-two-biggest-squares x y z)
  (apply + (map (lambda (x) (* x x))
                (filter (lambda (x) (not (eq? x (min x y z)))) (list x y z)))))


but this fails on certain types of input.

Name: Anonymous 2009-09-05 10:29

>>48

You do realise I just grabbed your dick right? *grabs dick*

Name: Anonymous 2009-09-05 10:29

>>50,51
We should have an artist illustrate it.

Name: Anonymous 2009-09-05 10:31

(define (sum-two-biggest-squares x y z)
  (let ((sorted (sort (list x y z) >))
        (square (lambda (x) (* x x))))
    (+ (square (car sorted))
       (square (cadr sorted)))))

Name: Anonymous 2009-09-05 12:09

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.

Name: Anonymous 2009-09-05 13:22

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

Name: Haxus the C-Lisper 2009-09-05 19:43

#define MIN( a , b ) ((a<b)?a:b)
#define MAX( a , b ) ((a>b)?a:b)

#define SQUARE( x ) (x*x)

uint32_t ex3(uint32_t x, uint32_t y, uint32_t z)
{
        return (SQUARE(MAX(x,y)) + (SQUARE(MAX(MIN(x,y),z))));
}

Name: Anonymous 2009-09-05 23:43

(((7 8 +) 5 /) e -)

Name: Anonymous 2009-09-06 1:21

>>61
Is it Forth, or is it Lisp? FITHP!

Name: Anonymous 2009-09-06 3:22


function gsq(a,b,c) return a^2 + b^2 + c^2 - math.min(a, b, c)^2 end

amidoinitrite?

Name: Anonymous 2009-09-06 5:09

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

Name: Anonymous 2009-09-06 5:47

(͝define (͏ęx͡3̀ x҉ ͟y z)͡
̧ ̷ ҉ ̡ ͟ ̡ ̸ ͠ ́ ̸ ͟ ͠(c̡on̨d
̴ ͢ ̛ ҉ ̛ ̢ ̢ ͟ ҉ ͡ ̧ ̷ ̸ ̡ ͢ ̴ ̡(̢(̨>͞ y z͘ x̵)̢ ͜((҉de͝fi̷ǹe a ͘x)̵ (͠d̨e̕f̀in͠e ͟b̛ ̵y̴)̷ (+ (s̨qu̸are a͡)̨ (sq͝ua̕rȩ b))͝)̡)
̕ ̕ ͝ ҉ ҉ ͜ ̶ ̵ ̢ ((͜>̵ y x́ ź)҉ ͘(͝(̴def͘i͢n͞e ̀a x)͞ (defi͜n͝e͢ b y͢) ̢(+ ̨(͜śq̛u̢ar͠e ̕a͏)͝ ͘(s҉q͜u̸àre͢ ̴b))͢))
͟ ̨ ̢ ́ ̵ ̢ ͟ ͝ ͡(͡(͟>̡ z̢ y x͠) (́(́d͘efi͘ņe͘ a ͠x̴)́ (͏defi̷n͞e b ̧y̷)̨ (̨+ (͝sq͞u̸áre ̸a̧)͢ ̡(s̶q͠uar̵e̸ b̶)͞)̧)̧)
̴ ̷ ͏ ̢ ̢ ̵ ̷ ̶ ̧((> ̧z x y)̀ (҉(͝d͞ef̵in͢e ͢a x͠)͡ (d̷ef͜i͏nę ̶b͏ ̧ỳ)҉ ͟(+ ͢(̸s̡q̡uare ̨a͠) (sq̢u͞are ̵b))))̡
̢ ̷ ͝ ̶ ̸ ̨ ̸ ͠ ̀ ̸ ҉ ͘ ((͘≯ x z̸ ̸y) ̀(҉(d́efi҉ne a ̨x) ͟(͜de͘fi̶n̛e ̕b y) ̨(͡+ ͟(sq͘uar̛e̛ a)͝ ͠(̢s̵q̢ua̶re ́b͜))̷)̀)
̡ ̕ ͘ ̕ ̕ ̸ ͜ ̕ ̷ ͘ ̷ ̡ ̕ ((> x ҉y z) ((define a ̕x͝) ́(de̸fin͢e͘ b y) ̛(+͘ (̛s̀q͜u̢a͘re a̷) ̨(̛squar͏e ̕b))))̢)̵

Name: Anonymous 2009-09-06 5:48

>>64
We're doing programming textbook exercises here, not enterprise scalable work

Name: Anonymous 2009-09-06 7:37

>>66

We're doing toy programs here ..

oh I see

Name: Anonymous 2009-09-06 7:47

>>67
Most textbook exercises ask you to write toy programs so you could further your knowledge. I don't see a problem with that.

Name: Anonymous 2009-09-06 10:04

>>65
VALID PERL CODE

Name: Anonymous 2009-09-06 14:16

>>65

Why did you scribble all over my code?!

Name: Anonymous 2009-10-25 19:14

>>51
pix plox

Name: QBasic Master 2009-10-29 1:33

>>60
I like this one best.

I will now attempt to solve this in QBasic.

DECLARE FUNCTION MAX% (x%, y%)
DECLARE FUNCTION MIN% (x%, y%)
DECLARE FUNCTION SQUARE% (x%)

FUNCTION MAX% (x%, y%)
    IF x% > y% THEN MAX% = x% ELSE MAX% = y%
END FUNCTION

FUNCTION MIN% (x%, y%)
    IF x% < y% THEN MAX% = x% ELSE MAX% = y%
END FUNCTION

FUNCTION SQUARE% (x%)
    SQUARE% = x% * x%
END FUNCTION


' This is the main program
CLS

INPUT "Enter 3 integers: ", a%, b%, c%
PRINT
PRINT SQUARE%(MAX%(a%, b%)) + SQUARE%(MAX%(MIN%(a%, b%), c%))

END


Didn't run it in the interpreter yet but it should work to the best of my knowledge.

Name: QBasic Master 2009-10-29 1:40

ah shit, already see an error in MIN%.  will fix it when I actually run the code.

Name: ty 2010-10-05 7:07

(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))))

works with all combinations of input right

Name: Anonymous 2010-10-05 7:46

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: Anonymous 2010-10-05 9:16

lambda *n: sum(map(lambda x: x * x, sorted(n)[-2:]))

Name: Anonymous 2010-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: Anonymous 2010-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: Anonymous 2010-10-05 11:19

>>78
...and yet cleaner, easier to read and much more intelligible than all the other Lithpth code posted. Feature that.

Name: Anonymous 2010-10-05 11:32

X←6 3 4
+/2*1↓X[⍋X]

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