Jerry  Zhao
Jerry Zhao

Reputation: 285

A elementary Lisp procedure error

(define (sum-two-sqrt a b c)

    (cond ((and (<= c a) (<= c b)) sqrt-sum(a b))
           ((and (<= a b) (<= a c)) sqrt-sum(b c))
           ((and (<= b a) (<= b c)) sqrt-sum(a c))
    )
)
(define (sqrt-sum x y)
           (+ (* x x) (*y y))
)
(define (<= x y)
      (not (> x y))

(sum-two-sqrt 3 4 5)

This is my code

Please help me to fix the problem. :)

I just start studing Lisp today.

learned some C before but the two language is QUITE DIFFERENT!

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

If you have better algorithm

POST IT!

Thank you :)

Upvotes: 3

Views: 210

Answers (4)

user1651640
user1651640

Reputation: 181

the algorithm seems to work, just turn

*y

to

* y

whitespace is important here, else you're telling the interpreter you want to usethe function *y

add a close paren after

(define (<= x y) (not (> x y))

sqrt-sum(a b) 

turns to

(sqrt-sum a b)

and ditto for the other sqrt-sum calls

edit: also a possibility:

(define (square a) (* a a))
(define (square-sum a b c)
    (- (+ (square a) 
          (square b)
          (square c))
       (square (min a b c))))

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236150

There's no need to define <=, it's a primitive operation. After fixing a couple of typos:

  • sqrt-sum: you were incorrectly invoking the procedure; the opening parenthesis must be written before the procedure name, not after.
  • sqrt-sum: (*y y) is incorrect, you surely meant (* y y); the space(s) after an operator matter.

This should work:

(define (sqrt-sum x y)
  (+ (* x x) (* y y)))

(define (sum-two-sqrt a b c)
  (cond ((and (<= c a) (<= c b)) (sqrt-sum a b))
        ((and (<= a b) (<= a c)) (sqrt-sum b c))
        ((and (<= b a) (<= b c)) (sqrt-sum a c))))

Or another alternative:

(define (sum-two-sqrt a b c)
  (let ((m (min a b c)))
    (cond ((= a m) (sqrt-sum b c))
          ((= b m) (sqrt-sum a c))
          (else (sqrt-sum a b)))))

Upvotes: 3

user797257
user797257

Reputation:

I didn't have Scheme interpreter here, but below seems to be shorter then other suggestions :) So it's in CL, but should look very similar in Scheme.

(defun sum-two-sqrt (a b c)
  (let ((a (max a b))
        (b (max (min a b) c)))
    (+ (* a a) (* b b))))

In Scheme this would translate to:

(define (sum-two-sqrt a b c)
  (let ((a (max a b))
        (b (max (min a b) c)))
    (+ (* a a) (* b b))))

Upvotes: 0

John Clements
John Clements

Reputation: 17233

Following up on a suggestion by @J.Spiral and seconded by @River, the following Racket code reads nicely to me:

#lang racket

(define (squares-of-larger l)
  (define two-larger (remove (apply min l) l))
  (for/sum ([i two-larger]) (* i i)))

(squares-of-larger '(3 1 4)) ;; should be 25

Please note that this solution is entirely functional, since "remove" just returns a new list.

Also note that this isn't even in the same neighborhood with HtDP; I just wanted to express this concisely, and show off for/sum.

Upvotes: 1

Related Questions