Very simple lisp, help

Discussion of Common Lisp
Post Reply
brogers614

Very simple lisp, help

Post by brogers614 » Wed Dec 16, 2009 11:05 am

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))

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Very simple lisp, help

Post by ramarren » Sun Dec 20, 2009 2:15 am

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.

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Very simple lisp, help

Post by gugamilare » Sun Dec 20, 2009 7:14 pm

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)

Post Reply