Member Avatar

dmmckelv

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

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

Member Avatar

dmmckelv

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.

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.

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