Page 1 of 1

Simple program

Posted: Sun Sep 13, 2009 8:03 am
by maciex3
What is wrong in this program:
(setq x (read))
(define y x)
(if (eql y 1) (write "1"))
(else (write "2"))
?

Re: Simple program

Posted: Sun Sep 20, 2009 1:20 am
by findinglisp
1. It looks like maybe it's Scheme code hanging out in the Common Lisp forum. Please make sure you post to the appropriate forum.

2. Lisp doesn't have an ELSE form. ELSE is typically just done as the third sub-form of an IF form. e.g: (IF <condition> <then-form> <else-form>)

What error are you getting?

Re: Simple program

Posted: Sun Sep 20, 2009 8:52 am
by Jasper
You're trying to do things as if it is C or C++, lisp isn't written like that.

Code: Select all

(let*((x (read)) (y x)) ;LET for local variables, LET* is the variant where the order matters. (y is pretty pointless)
  (if (eql y 1) (write "1") ;Note that print is more usually used for stuff like this.
                   (write "2")))
Note that (read) tries to read from T, it needs user input!

What are you learning with? PCL is pretty good.

As for what is wrong; DEFINE doesn't exist defaultly in CL, SETQ should only be used on already existing variables. ELSE doesn't exist either; it should be done as the second clause of IF. (Or same effect with other macros like COND, CASE etcetera.)