This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Simple program

3 posts · 1154 views

What is wrong in this program:
(setq x (read))
(define y x)
(if (eql y 1) (write "1"))
(else (write "2"))
?

Re: Simple program

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?
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

Re: Simple program

You're trying to do things as if it is C or C++, lisp isn't written like that.
(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.)