Hi,
I have the first ed. of the book and also am following the MIT lecture
videos on line. I was doing the 1st chapter exercises and got stuck at
ex 1.3 which asks to take the sum of the squares of the 2 larger numbers
of 3 given. Here's how I tackled it...It shows my procedural thinking
while a more elegant solution probably involves recursion based on the
theme of the first chapter...but I am unable to see it.
First I defined a function to take larger of two numbers (easy)
(defun lof2 (x y)
(if (> x y) x y))
Then I use it to take the larger 2 out of 3 numbers
(defun l2of3 (x y z)
(let
((a (lof2 x y))
(b (lof2 y z))
(c (lof2 x z)))
(if (= a b) (list a c) (list a b))))
A function to take sum of squares of 2 nums...
(defun sum-sq (x y)
(+ (* x x)(* y y)))
The final step is giving me a lot of head aches...
(defun ex13 (x y z)
(sum-sq (l2of3 x y z)))
This does not work and I get an error message complaining that sum-sq is
expecting 2 arguments and it is getting only one.
Here's what I don't understand..
If I do (l2of3 3 4 5) I get
(4 5)
Aren't arguments passed to a function passed as a list? I am returning a
list from l2of3, so why does lisp complain? If arguments passed are NOT
a list how can I return the results from l2of3 to sum-sq?
I am also reading Seibel's book and OnLisp at the same time and couldn't
find an explanation. What am I missing? I appreciate any help and also a
solution based on recursion for this problem.
Thanks in advance,
bakki
>The final step is giving me a lot of head aches...
>
>(defun ex13 (x y z)
> (sum-sq (l2of3 x y z)))
>
>This does not work and I get an error message complaining that sum-sq is
>expecting 2 arguments and it is getting only one.
>
>Here's what I don't understand..
>
>If I do (l2of3 3 4 5) I get
>(4 5)
>
>Aren't arguments passed to a function passed as a list?
No. If they were how would l2of3 be working? You'd need
to pass it the list (3 4 5) instead of three separate arguments, 3, 4,
and 5.
> I am returning a
>list from l2of3, so why does lisp complain? If arguments passed are NOT
>a list how can I return the results from l2of3 to sum-sq?
>
(apply 'sum-sq (l2of3 x y z))
>I am also reading Seibel's book and OnLisp at the same time and couldn't
>find an explanation. What am I missing? I appreciate any help and also a
>solution based on recursion for this problem.
>
>Thanks in advance,
>
>bakki
JP Massar wrote:
>
>> Aren't arguments passed to a function passed as a list?
>
> No. If they were how would l2of3 be working? You'd need
> to pass it the list (3 4 5) instead of three separate arguments, 3, 4,
> and 5.
>
>> I am returning a
>> list from l2of3, so why does lisp complain? If arguments passed are NOT
>> a list how can I return the results from l2of3 to sum-sq?
>>
>
> (apply 'sum-sq (l2of3 x y z))
Thank you very much for clearing up my confusion.
I thought that the values were passed as a list because of the parens
around them in defun. Should have known that since there are no parens
when you call the function the params are indeed seperate values.
-bakki
Bakki Kudva wrote:
> Hi,
>
> I have the first ed. of the book and also am following the MIT lecture
> videos on line. I was doing the 1st chapter exercises and got stuck at
> ex 1.3 which asks to take the sum of the squares of the 2 larger numbers
> of 3 given. Here's how I tackled it...It shows my procedural thinking
> while a more elegant solution probably involves recursion based on the
> theme of the first chapter...but I am unable to see it.
>
> First I defined a function to take larger of two numbers (easy)
>
> (defun lof2 (x y)
> (if (> x y) x y))
>
> Then I use it to take the larger 2 out of 3 numbers
>
> (defun l2of3 (x y z)
> (let
> ((a (lof2 x y))
> (b (lof2 y z))
> (c (lof2 x z)))
> (if (= a b) (list a c) (list a b))))
>
> A function to take sum of squares of 2 nums...
>
> (defun sum-sq (x y)
> (+ (* x x)(* y y)))
>
> The final step is giving me a lot of head aches...
>
> (defun ex13 (x y z)
> (sum-sq (l2of3 x y z)))
>
> This does not work and I get an error message complaining that sum-sq is
> expecting 2 arguments and it is getting only one.
>
> Here's what I don't understand..
>
> If I do (l2of3 3 4 5) I get
> (4 5)
>
> Aren't arguments passed to a function passed as a list? I am returning a
> list from l2of3, so why does lisp complain? If arguments passed are NOT
> a list how can I return the results from l2of3 to sum-sq?
>
> I am also reading Seibel's book and OnLisp at the same time and couldn't
> find an explanation. What am I missing? I appreciate any help and also a
> solution based on recursion for this problem.
>
> Thanks in advance,
>
> bakki
newLisp:
(define (l2of3)
(let (lst (args))
(pop lst (find (apply min lst) lst))
lst))
(define (sum-sq x y)
(+ (* x x)(* y y)))
(define (ex13 x y z)
(apply sum-sq (l2of3 x y z)))
matzLisp (a.k.a. Ruby):
def l2of3( *args )
args.delete_at( args.index( args.min ))
args
end
def sum_sq( x, y )
x*x + y*y
end
def ex13( x, y, z )
sum_sq( *l2of3( x, y, z ) )
end
William James <·········@yahoo.com> wrote:
> matzLisp (a.k.a. Ruby):
[... snip the Ruby code ... ]
Please stop this nonsense. People asking questions about Lisp are
probably not interested in getting answers about Ruby. I don't know
why you keep pretending Ruby is a Lisp, but it isn't. It doesn't look
like a Lisp, it doesn't quack like a Lisp, and it isn't on-topic in
comp.lang.lisp.
--
Juho Snellman
Bakki Kudva <···········@gmail.com> writes:
> I have the first ed. of the book and also am following the MIT lecture
> videos on line. I was doing the 1st chapter exercises and got stuck at
> ex 1.3 which asks to take the sum of the squares of the 2 larger
> numbers of 3 given.
That's funny, this is always this exercise newbies ask about...
> Here's how I tackled it...It shows my procedural
> thinking while a more elegant solution probably involves recursion
> based on the theme of the first chapter...but I am unable to see it.
>
> First I defined a function to take larger of two numbers (easy)
>
> (defun lof2 (x y)
> (if (> x y) x y))
>
> Then I use it to take the larger 2 out of 3 numbers
>
> (defun l2of3 (x y z)
> (let
> ((a (lof2 x y))
> (b (lof2 y z))
> (c (lof2 x z)))
> (if (= a b) (list a c) (list a b))))
The "elegant" way to write this function is to note that the larger 2
out of 3 don't include the smaller of them.
(defun l2of3 (x y z)
(if (< x y)
(if (< z x)
(list x y) ; z<x<y
(list y z)) ; x<=z & x<y
(if (< z y)
(list x y) ; z<y<=x
(list x z)))) ; y<=z & y<=x
Thus you get the result with only two tests.
In your implementation, you're making four tests (three < and one =).
> [...]
JP answered your other question, but note that without losing
generality, you can write l2of3 differently, and combine it with
sum-sq:
(defun l2of3 (x y z cont)
(if (< x y)
(if (< z x)
(funcall cont x y) ; z<x<y
(funcall cont y z)) ; x<=z & x<y
(if (< z y)
(funcall cont x y) ; z<y<=x
(funcall cont x z)))) ; y<=z & y<=x
(l2of3 2 3 4 (function sum-sq)) --> 25 ; = 9+16
(l2of3 2 3 4 (function list)) --> (3 4)
The name of the parameter "CONT" comes from "continuation", which is
an interesting low-level notion in Scheme (while we don't have full
continuations in Common Lisp we can still implement or use partial
continutations, or at least the ideas).
--
__Pascal Bourguignon__ http://www.informatimago.com/
Wanna go outside.
Oh, no! Help! I got outside!
Let me back inside!