Page 1 of 1

Very simple lisp, help

Posted: Wed Dec 16, 2009 11:05 am
by brogers614
I have a very simple code problem I trying to find the LCM of few numbers I first created this code which compiles and works..

(defun lcm (&key a b c d e) (lcm a b c d e ))
(lcm :a 3 :b 2 :c 4 :d 7 :e 11)

My instructor informed me that I needed to use &rest. Every time I tried to use the &rest function it has not worked, I am not sure if my syntax is wrong or not. Bellow are the 2 ways I think the code should work but is not working. ANy help or suggestions Thanks much... I

(defun my-lcm (&rest a b c d e) (lcm a b c d e ))
(my-lcm :a 3 :b 2 :c 4 :d 7 :e 11)

Or

(defun my-lcm (&rest 1 2 3 4 5))

Re: Very simple lisp, help

Posted: Sun Dec 20, 2009 2:15 am
by ramarren
Somehow I doubt that your instructor meant for you to just wrap the standard LCM function. In any case, why are you using keyword arguments? They make no sense in this context. Just use a single &REST argument. This is fairly elementary Common Lisp, you should probably read at least some parts of Practical Common Lisp.

Re: Very simple lisp, help

Posted: Sun Dec 20, 2009 7:14 pm
by gugamilare
What you might be trying to do (not tested!):

Code: Select all

(defun lcm (&key a b c d e &rest other-keys)
  (list a b c d e (getf other-keys :f)))

(lcm :a 1 :b 2 :c 3 :d 4 :e 5 :f 6) => (1 2 3 4 5 6)