The prompt is to define a procedure that returns the sum of the squares of the two largest of three numbers.
I know this isn’t exactly an elegant solution, but this is what I hacked together:
(define (largest-of-two-sum-of-squares x y z)
(cond ((and (< x y) (< x z)) (sum-of-squares y z))
((and (< y z) (< y x)) (sum-of-squares x z))
((and (< z x) (< z y)) (sum-of-squares x y)))))
What I’m wondering is why I’m getting an error.
;The object 85 is not applicable
The number that follows the word object is always the correct answer, btw. I am a scheme beginner, it must be something in my syntax?
Thanks
asked Mar 10, 2012 at 17:10
2
Here’s another possible solution, this one works even in the cases where all three numbers are equal or if two are equal and lower than the other:
(define (sum-max a b c)
(define (sum x y)
(+ (* x x) (* y y)))
(if (>= a b)
(if (>= b c)
(sum a b)
(sum a c))
(if (>= a c)
(sum b a)
(sum b c))))
answered Mar 10, 2012 at 17:36
![]()
Óscar LópezÓscar López
230k37 gold badges309 silver badges383 bronze badges
1
As sindikat pointed out, an excess closing bracket. Sorry about that.
answered Mar 10, 2012 at 17:18
MikeMike
9534 gold badges15 silver badges38 bronze badges
1
What about
(define (largest-of-two-sum-of-squares x y z)
(+ (square x) (square y) (square z)
(- (square (min x y z)))))
?
answered Mar 11, 2012 at 22:17
65026502
111k15 gold badges159 silver badges263 bronze badges
1
The prompt is to define a procedure that returns the sum of the squares of the two largest of three numbers.
I know this isn’t exactly an elegant solution, but this is what I hacked together:
(define (largest-of-two-sum-of-squares x y z)
(cond ((and (< x y) (< x z)) (sum-of-squares y z))
((and (< y z) (< y x)) (sum-of-squares x z))
((and (< z x) (< z y)) (sum-of-squares x y)))))
What I’m wondering is why I’m getting an error.
;The object 85 is not applicable
The number that follows the word object is always the correct answer, btw. I am a scheme beginner, it must be something in my syntax?
Thanks
asked Mar 10, 2012 at 17:10
2
Here’s another possible solution, this one works even in the cases where all three numbers are equal or if two are equal and lower than the other:
(define (sum-max a b c)
(define (sum x y)
(+ (* x x) (* y y)))
(if (>= a b)
(if (>= b c)
(sum a b)
(sum a c))
(if (>= a c)
(sum b a)
(sum b c))))
answered Mar 10, 2012 at 17:36
![]()
Óscar LópezÓscar López
230k37 gold badges309 silver badges383 bronze badges
1
As sindikat pointed out, an excess closing bracket. Sorry about that.
answered Mar 10, 2012 at 17:18
MikeMike
9534 gold badges15 silver badges38 bronze badges
1
What about
(define (largest-of-two-sum-of-squares x y z)
(+ (square x) (square y) (square z)
(- (square (min x y z)))))
?
answered Mar 11, 2012 at 22:17
65026502
111k15 gold badges159 silver badges263 bronze badges
1
Подсказка состоит в том, чтобы определить процедуру, которая возвращает сумму квадратов двух наибольших из трех чисел.
Я знаю, что это не совсем элегантное решение, но это то, что я взломал вместе:
(define (largest-of-two-sum-of-squares x y z)
(cond ((and (< x y) (< x z)) (sum-of-squares y z))
((and (< y z) (< y x)) (sum-of-squares x z))
((and (< z x) (< z y)) (sum-of-squares x y)))))
Мне интересно, почему я получаю сообщение об ошибке.
;The object 85 is not applicable
Кстати, число, которое следует за словом «объект», всегда является правильным ответом. Я новичок в схеме, это должно быть что-то в моем синтаксисе?
Благодарность
3 ответа
Вот еще одно возможное решение, оно работает даже в тех случаях, когда все три числа равны или если два равны и меньше другого:
(define (sum-max a b c)
(define (sum x y)
(+ (* x x) (* y y)))
(if (>= a b)
(if (>= b c)
(sum a b)
(sum a c))
(if (>= a c)
(sum b a)
(sum b c))))
3
Óscar López
11 Мар 2012 в 18:29
Как указал синдикат, лишняя закрывающая скобка. Извини за это.
1
Mike
10 Мар 2012 в 21:18
Что о
(define (largest-of-two-sum-of-squares x y z)
(+ (square x) (square y) (square z)
(- (square (min x y z)))))
?
1
6502
12 Мар 2012 в 02:17
As other answers pointed out, #( is the syntax to represent the ( character. See section 2.4.8 Sharpsign. First the reader sees #, then dispatches on the next character, (backslash) which is used to read character objects. Your error message is thus saying that ) was unexpected in your input.
Error message
I was expecting some error message like umatched parenthesis.
This is basically what the error is saying, and other implementations like SBCL produce the message you expect. I am working from Emacs:
CL-USER> )
… enters the debugger with unmatched close parenthesis and a backtrace.
Let’s see why. Using GET-MACRO-CHARACTER, we ask our Common Lisp implementation to return the function associated with the #) character:
CL-USER> (get-macro-character #))
SB-IMPL::READ-RIGHT-PAREN
NIL
In Emacs, the resulting value is a shown as a presentation, which allows us to inspect it. You can also point the name of the function, and if your installation of SBCL allows it (e.g. you installed from source), you can go to the definition with M-.:
(defun read-right-paren (stream ignore)
(declare (ignore ignore))
(simple-reader-error stream "unmatched close parenthesis"))
The same with CLISP produces the error you see. We can also do:
CL-USER> (get-macro-character #))
#<SYSTEM-FUNCTION SYSTEM::RPAR-READER>
NIL
However the source definition for that system function is not so clear (if you are curious see the non-official mirror), but it is not surprising that another error message is displayed.
The read-table and how Lisp reads lists of expressions
The behavior depends on which functions is bound in the current readtable to the character being read.
All implementations signal an error in that case, because when you reach a point where you have to lookup into the readtable for a closing parenthesis, the input is necessarily badly formed. It is the job of a function like READ-DELIMITED-LIST, called from the function associated with the opening parenthesis, a.k.a. #(, to read until it find the corresponding closing parenthesis. Note that if you look-up the reader for #( in SBCL, it does not use read-delimited-list, but a different, specialized version. It is a little bit too long to post it here.
Quote and recursive reading
In your case, you also have quote. How does quote behave?
(defun read-quote (stream ignore)
(declare (ignore ignore))
(list 'quote (read stream t nil t)))
It builds a list starting with quote and containing a sub-expression obtained by calling READ recursively. This is just as above, when I entered a closing parenthesis at the REPL prompt. Since we are not currently in the context of an open parenthesis, we encounter the #) character, look it up in the readtable and signal an error.
As other answers pointed out, #( is the syntax to represent the ( character. See section 2.4.8 Sharpsign. First the reader sees #, then dispatches on the next character, (backslash) which is used to read character objects. Your error message is thus saying that ) was unexpected in your input.
Error message
I was expecting some error message like umatched parenthesis.
This is basically what the error is saying, and other implementations like SBCL produce the message you expect. I am working from Emacs:
CL-USER> )
… enters the debugger with unmatched close parenthesis and a backtrace.
Let’s see why. Using GET-MACRO-CHARACTER, we ask our Common Lisp implementation to return the function associated with the #) character:
CL-USER> (get-macro-character #))
SB-IMPL::READ-RIGHT-PAREN
NIL
In Emacs, the resulting value is a shown as a presentation, which allows us to inspect it. You can also point the name of the function, and if your installation of SBCL allows it (e.g. you installed from source), you can go to the definition with M-.:
(defun read-right-paren (stream ignore)
(declare (ignore ignore))
(simple-reader-error stream "unmatched close parenthesis"))
The same with CLISP produces the error you see. We can also do:
CL-USER> (get-macro-character #))
#<SYSTEM-FUNCTION SYSTEM::RPAR-READER>
NIL
However the source definition for that system function is not so clear (if you are curious see the non-official mirror), but it is not surprising that another error message is displayed.
The read-table and how Lisp reads lists of expressions
The behavior depends on which functions is bound in the current readtable to the character being read.
All implementations signal an error in that case, because when you reach a point where you have to lookup into the readtable for a closing parenthesis, the input is necessarily badly formed. It is the job of a function like READ-DELIMITED-LIST, called from the function associated with the opening parenthesis, a.k.a. #(, to read until it find the corresponding closing parenthesis. Note that if you look-up the reader for #( in SBCL, it does not use read-delimited-list, but a different, specialized version. It is a little bit too long to post it here.
Quote and recursive reading
In your case, you also have quote. How does quote behave?
(defun read-quote (stream ignore)
(declare (ignore ignore))
(list 'quote (read stream t nil t)))
It builds a list starting with quote and containing a sub-expression obtained by calling READ recursively. This is just as above, when I entered a closing parenthesis at the REPL prompt. Since we are not currently in the context of an open parenthesis, we encounter the #) character, look it up in the readtable and signal an error.