Lisp question

Please support our Legacy and Other Languages advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2006
Posts: 32
Reputation: dmmckelv is an unknown quantity at this point 
Solved Threads: 0
dmmckelv dmmckelv is offline Offline
Light Poster

Lisp question

 
0
  #1
Jun 13th, 2007
I am trying to write a simple Lisp function for my Articficial Intelligence class and am running into a problem. The compiler is seeing the line [inline] format t "Enter a number, press return and enter another number"[/inline] as a an error. It is the placement of the line of code that is causing this error because if I move it, it will display but, I need it to start the function. I am sure it is something simple that I am missing. Please help

(defun adder()((format t "Enter a number, press return and enter another number")(setq numberOne (read)) (setq numberTwo (read))(format t "~A plus ~A equals ~A" numberOne numberTwo (+ numberOne numberTwo))))

Here is the error it is giving me: (FORMAT T "Enter a number, press return and enter another number"). [condition type: TYPE-ERROR]


Thanks a bundle. ( I hope the code tags worked.)
Last edited by dmmckelv; Jun 13th, 2007 at 9:56 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 71
Reputation: indienick is an unknown quantity at this point 
Solved Threads: 2
indienick's Avatar
indienick indienick is offline Offline
Junior Poster in Training

Re: Lisp question

 
1
  #2
Jun 14th, 2007
It appears you have a superfluous opening parenthesis just before the FORMAT call:
(defun adder ()
  (format t "Enter a number, press return and enter another number")
  (setq number-one (read))
  (setq number-two (read))
  (format t "~a plus ~a equals ~a" number-one number-two (+ number-one number-two)))
Also, more tips for Lisp programming:

-Remember that the REPL automatically changes all S-expressions to upper case; leave capital letters out of variable names and out of FORMAT directives - "~a" for example.

-Format your code nicely - it makes it SO much easier to read (for everyone, most of all yourself).

EDIT: I also just noticed, something (silly me).

(defun adder ()
  (format t "Enter a number, press return and enter another number")
  (setq number-one (parse-integer (read)))
  (setq number-two (parse-integer (read)))
  ...)
Note the use of PARSE-INTEGER.

Also, which Lisp implementation are you using? I haven't seen SETQ used outside of lexical bindings in a while (unless you're using Scheme, or a REALLY old implementation; pre-Common Lisp).
Last edited by indienick; Jun 14th, 2007 at 12:27 am.
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg

Don't tell me to "google it" - I already have.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 32
Reputation: dmmckelv is an unknown quantity at this point 
Solved Threads: 0
dmmckelv dmmckelv is offline Offline
Light Poster

Re: Lisp question

 
0
  #3
Jun 14th, 2007
One little sneaky parenthesis can cause a lot of pain in LISP. You solved my problem. Thanks.

I could not implement the parse-integer, it gave me a type error.

Also, the other tips were helpful. They have made the rest of my homework look much better as well.

I am using Common-Lisp I am using International Allegro CL Free Express Edition 8.0 as my editor. Maybe the instructor is just old-school. This is my first introduction to LISP.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 36
Reputation: azimuth0 is an unknown quantity at this point 
Solved Threads: 5
azimuth0 azimuth0 is offline Offline
Light Poster

Re: Lisp question

 
0
  #4
Jun 14th, 2007
The READ function will return a lisp object just like the representation in your code, but PARSE-INTEGER only takes strings. You probably want to use READ-LINE instead, which returns only strings.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 71
Reputation: indienick is an unknown quantity at this point 
Solved Threads: 2
indienick's Avatar
indienick indienick is offline Offline
Junior Poster in Training

Re: Lisp question

 
0
  #5
Jun 14th, 2007
You're very welcome, dmmckelv.

azimuth0: Thanks for the additional info - I've never used READ, and just assumed it returned any expression as a string type, and not the entire line. Bad call on my part. (My thinking is similar to the difference between INPUT and LINE INPUT in BASIC dialects).
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg

Don't tell me to "google it" - I already have.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Legacy and Other Languages Forum


Views: 1632 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Legacy and Other Languages
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC