;; Exercise 1.3

;; 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 (square x) (* x x))
(define (sum-of-squares x y) (+ (square x) (square y)))

(define (sum-two-largest-squares x y z)
(if (> x y)
(if (> y z) (sum-of-squares x y) (sum-of-squares x z))
(if (> x z) (sum-of-squares x y) (sum-of-squares y z))))

Attached: 1537871406132.png (724x674, 491K)

Other urls found in this thread:

more-magic.net/posts/internals-gc.html
twitter.com/SFWRedditImages

i don't fucking understand this shit

It's simpler in structure than other programming languages.

What exactly don't you understand?

Are you stupid or do you just not understand lisp's polish notation? It's doing middle school math with 2 simple functions and 3 simple if statements.

*reverse-polish notation.

So this person wrote another if statement as a predicate under (if )

??
I just started today ok, I feel really stupid already. I have to freaking create a tree just to follow their if statements.

Actually no, Lisp uses Polish notation.

lisp makes sense, it would look good too if it wernt for all the paren. cant do anything about that tho. Also using rpn keeps the tards out of your language

>another if statement as a predicate
no they didn't

polish, not rpn. tard

>have to freaking create a tree just to follow their if statements
this is a good exercise

> I have to freaking create a tree just to follow their if statements.
You have to do that anyway, you know.

sum . (map square) . (take 2) . sort

(if (> x y)
>if true go to second line, if false go to third line?

reverse polish has the operators after.
You probably shouldn't start with sicp and lisp if you have trouble with if statements.

but i gotta

Attached: 1541206527540.jpg (800x534, 101K)

ur mum uses polish

I figured it out using a tree but the format confuses me still.

You can use one less if

Thanks, anything to make my answer not look like it was copied from a female.

Attached: 1535409928927.jpg (848x1024, 90K)

de nada
Need hints?

Lisp dialects are most expressive when you break things apart into small functions.
Calling functions or passing functions is not expensive like it is in most other languages.
Make a min functions and a max function and a max-two function and a square function and a sum-square function and suddenly it's done in "one line", but you're left with a collection of expressions that potentially can be used elsewhere because of how simple they are.

If only it wasn't in reverse order.

stop posting dog abusers

fn f(n1: T, n2: T, n3: T) -> ::Output
where
T: std::cmp::Ord + std::ops::Mul + std::clone::Clone,
::Output: std::ops::Add + std::fmt::Display,
{
let mut arr = [n1, n2, n3];
arr.sort();
return arr[2].clone() * arr[2].clone() + arr[1].clone() * arr[1].clone();
}

You may not like it, but this is what peak performance looks like

Attached: 1392011828135.png (502x463, 178K)

ur supposed to make it using chapter 1, if, else. and, cond

At what point is it just plain math? Like who would give a fuck about this algorithm in the real world?

Attached: pepe.jpg (487x498, 78K)

should have used cond instead, what is this shit

A girl wrote it on github, i know i know. im starting to realize that

jesus christ this is the ugliest thing i've ever seen

git gud, noobs

Attached: 1392011828136.png (556x837, 332K)

okay i figured it out

xy, xz, yz are only possible combinations

Wait why is this in postfix

Atrocious. Here's my common lisp version:
(defun square (x) (* x x))
(defun minimum (x y z)
(if (and (< x y) (< x z)) (return-from minimum x))
(if (> y z) (return-from minimum z) (return-from minimum y)))
(defun sum-square-of-max-two (x y z)
(if (= (minimum x y z) z) (return-from sum-square-of-max-two (+ (square x) (square y))))
(if (= (minimum x y z) y) (+ (square x) (square z)) (+ (square y) (square z))))

Squares of what? What the fuck is a square x?

Square root? Squared? Square with x = length of each side? Square named x?

(sum-two-largest-squares 3 2 5)

(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))

(define (sum-two-largest-squares x y z)
(cond ((and (> x y) (> y z)) sum-of-squares x y)
((and (> y x) (> z x)) sum-of-squares y z)
((and (> x y) (> z y)) sum-of-squares x z)))

what did i do wrong?

only prints z value

const highestSquares = (...args) => {
// get the 2 highest memers
const [high, mid] = args.sort((a, b) => b - a)
return Math.pow(high, 2) + Math.pow(mid, 2)
}

console.log(highestSquares(12,8,29))


what am I missing here

Looks like a syntax problem to me.

correct, I fixed it, thanks

>sorting
>cloning
>performance
Check yourself before your shrek yourself.

>tfw you save 0.0000000000001us by not sorting a 3 number array

Only two comparisons, which is the most optimal.
((lambda (x y z)
(let ((a (lambda (x y z)
(let ((b (lambda (x y) (+ (* x x) (* y y)))))
(if (> z y) (b x z) (b x y))))))
(if (> x y) (a x y z) (a y x z))))
1 2 3)

iSore

Cloning primitive numbers is not expensive

>moving goalposts

I figured out the most efficient solution, without any branching, but I'm a dumb phoneposter right now.

Post it in the archive if the thread dies, please

I solved mine by using cond with three statements, 9 greater than statements with 3 xx combinations

Specifically warosu.org lol

The gist of it is you can build a number from >= comparisons of 1 and 2, 2 and 3 and 3 and 1, it's always going to be 110, 101 or 011, then you can use to access the solution from a pre-computed array of pairs representing the solutions and use values from those pairs to return the result. Should be like 5 lines in C.

Oh, 111 is possible as well but doesn't require any modifications to the method.

>most efficient
>compute multiple solutions to throw away

That means you fill the array in advance, dummy. It's only 8 values.

first sicp chapter is a new fizzbuzz?

There's no goalpost

>It's simpler in structure than other programming languages.
def sum_two_largest_squares(x, y, z):
a, b = filter(lambda i: i != min(x, y, z), [x, y, z])
return a**2 + b**2

Or, for readability:

def sum_two_largest_squares(x, y, z):
n = [x, y, z]
n.remove(min(n))
return n[0]**2 + n[1]**2

THIS BOOK FULL OF WRONG IDEAS
JUST LEARN BASIC, IF IT EASY FOR YOU SELECT SOME LANGIAGE WITH LIBRARIES

Attached: BBASIC_QUEENS_DIAGRAMM.png (763x785, 16K)

based

in ENDscript

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


PROC sq2largest(a b c)
WITH xy AS (a b c).orderby.largest
RET x**2 + y**2

semantically I don't like it. The code should isolate the two largest, not remove the lowest. If requirements were changed for the function to take 4 numbers it would be a problem.

>The code should isolate the two largest, not remove the lowest.
There is no difference....

>If requirements were changed for the function to take 4 numbers it would be a problem.
Depends, would it sum the three largest? In which case, removing the smallest is still the most sensible. Or would it still sum the two largest, in which case just remove the two smallest.

I would add a function two-largest-of-three. Sorry for bad style but this is what Little Schemer teaches, I bet there is some syntactic sugar to pick up:

(define two-largest-of-three
(lambda (x y z)
(cond
((< y z) (two-largest-of-three x z y))
((< x y) (two-largest-of-three y x z))
(else (list x y)))))


but I'm confused how should I approach the argument passing when this returns list of 2 numbers, but I want to pass the numbers directly and not wrapped in list. Or make the sum-two-largest-squares take list as argument.

Man do I ever wish that python had more of the higher functional concepts present in haskell. It would be the true language of the gods

But, if you hate it that much.

def sum_two_largest_squared(x, y, z):
n = sorted(x, y, z, reverse=True)
return n[0] ** 2 + n[1] ** 2

(define (sum-square-max a b c)
(define (square x) (* x x))
(define (sum-square x y)
(+ (square x)
(square y)))
(sum-square
(if (> a b) a b)
(if (> b c) b c)))

Have you ever read the Chicken Scheme dev's blogpost about how to express tail recursion in the outputted C code? It's mindblowing.

more-magic.net/posts/internals-gc.html

incorrect
if the biggest element is b, both ifs result in b

Dammit you sunk my battleship

good code, thanks