;; 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))))
It's simpler in structure than other programming languages.
Kevin Cooper
What exactly don't you understand?
Joseph Walker
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.
Julian Bailey
*reverse-polish notation.
Ian Murphy
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.
Chase Myers
Actually no, Lisp uses Polish notation.
Dominic Morgan
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
Nathaniel Sanders
>another if statement as a predicate no they didn't
Owen Myers
polish, not rpn. tard
Luke Turner
>have to freaking create a tree just to follow their if statements this is a good exercise
Dylan Morales
> I have to freaking create a tree just to follow their if statements. You have to do that anyway, you know.
Christian Lewis
sum . (map square) . (take 2) . sort
Gavin Martin
(if (> x y) >if true go to second line, if false go to third line?
Owen Phillips
reverse polish has the operators after. You probably shouldn't start with sicp and lisp if you have trouble with if statements.
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.
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))))
Luis Nguyen
Squares of what? What the fuck is a square x?
Square root? Squared? Square with x = length of each side? Square named x?
Jose Long
(sum-two-largest-squares 3 2 5)
Noah Richardson
(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?
Leo Richardson
only prints z value
Ayden Ward
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
Xavier Martin
Looks like a syntax problem to me.
Jeremiah Peterson
correct, I fixed it, thanks
Jeremiah Russell
>sorting >cloning >performance Check yourself before your shrek yourself.
Ian Torres
>tfw you save 0.0000000000001us by not sorting a 3 number array
Aaron Davis
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)
Bentley Jones
iSore
Jose Collins
Cloning primitive numbers is not expensive
Julian Robinson
>moving goalposts
Luke Sanchez
I figured out the most efficient solution, without any branching, but I'm a dumb phoneposter right now.
Christian Wilson
Post it in the archive if the thread dies, please
Gabriel Brooks
I solved mine by using cond with three statements, 9 greater than statements with 3 xx combinations
Christopher Bailey
Specifically warosu.org lol
Cooper Wilson
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.
Eli Collins
Oh, 111 is possible as well but doesn't require any modifications to the method.
Julian Jenkins
>most efficient >compute multiple solutions to throw away
Noah Miller
That means you fill the array in advance, dummy. It's only 8 values.
Liam Gutierrez
first sicp chapter is a new fizzbuzz?
Lucas Hernandez
There's no goalpost
Lucas Thomas
>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
Aaron Russell
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
Jeremiah Gomez
THIS BOOK FULL OF WRONG IDEAS JUST LEARN BASIC, IF IT EASY FOR YOU SELECT SOME LANGIAGE WITH LIBRARIES
;; 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
Wyatt Cox
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.
James Green
>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.
Robert Collins
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.
Ryder Gutierrez
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
Nathaniel Fisher
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
Logan Ward
(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)))
Jackson Butler
Have you ever read the Chicken Scheme dev's blogpost about how to express tail recursion in the outputted C code? It's mindblowing.