Simple program

Discussion of Common Lisp
Post Reply
maciex3

Simple program

Post by maciex3 » Sun Sep 13, 2009 8:03 am

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

findinglisp
Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX
Contact:

Re: Simple program

Post by findinglisp » Sun Sep 20, 2009 1:20 am

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/

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: Simple program

Post by Jasper » Sun Sep 20, 2009 8:52 am

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

Post Reply