# executing multiple statements with if/cond (Lisp)

by kataya
Tags: executing, if or cond, lisp, multiple, statements
 P: 23 I have been messing around with Lisp for a few days now, and I have run into a recurring problem with trying to execute multiple things on given conditions. In all other programming languages I have ever worked with, conditional blocks can always be defined with {} or some other delimiter, such that they can evaluate multiple statements on a given condition. To give a better example of what I mean, here is a simple function that I have been trying to write. ;;Returns the elements in xlist up to a given index (defun list-part (xlist index) (if (zerop index) nil ( (1- index) (cons (list (car xlist)) (list-part (cdr xlist))) ))) This will produce a compiler error saying that (1- index) is not the name of a function. Seeing how this fails, is there some what to execute multiple statements on a given condition? Thanks in advance for your help. EDIT: There are some fundamental errors in the function, such as calling list-part with only 1 argument. The function can in fact be made with with only 1 evaluation statement. However, is this always the case?
 P: 587 So let's say that in Lisp you say (a b c) This is actually a function call! This is like saying a(b,c) in C++. It's not like saying a;b;c. There's is some kind of builtin function or macro in LISP, I think, that takes its arguments and interprets (executes) them one by one. So you'd say (dothesethings a b c) and it would be like saying a;b;c; in some other language. However, I don't remember the name of this macro. But: It wouldn't help you anyway in this case! You have bigger problems. Your biggest problem here is that you seem to be doing things like (1- index) Or (cons (list (car xlist)) As if they actually modify the variables mentioned in them. They don't. Most Lisp expressions do not have side effects. For example I'm not familiar with the "1-" function, but if it's anything remotely normal then when you say (1- index) it will not subtract 1 from index, it will return the value of index minus one. If you put "(cons (list (car xlist))" alone on a line like that, meanwhile, the value will just get thrown away and nothing will change. So if I were in your situation, what I would do is just chain together "let" statements. I think "let" is probably the solution to both your problems. Let works like: ( let ( ( index (1- index) ) ; defines a new variable named 'index', assigns it index-1 ( xlist (cons (list (car xlist)) ) ; defines a new variable named 'xlist' (list-part (cdr xlist)) ) In each of the lines here you are defining a variable (you are NOT altering the value of 'index'-- you are binding a new variable, which exists only for the scope of the 'let', which has the value you assigned it) by the given name, which then exists in the successive lines. As far as I can tell this is what you were trying to do anyway. Incidentally be sure to double-check with your syntax or documentation because this is all from memory and I might not have the syntax right...! (If you find you *MUST* have something that acts like a traditional variable, where you can just plain alter the value and it changes, I think you can do this with 'set' and 'setq'.)
 P: 39 To answer the original question, assuming Common Lisp, you want to use prog (short for "program"), or its variations depending which value you want returned. For example: (if (zerop index) nil (progn (first-form) (second-form))) That example uses the progn variation of prog. It will evaluate all the contained forms in order and yield the value of last one (second-form in this case). progn is probably the most common way of evaluating multiple forms. Then there's prog1 and prog2, which behave similarly except they yield the first or second value rather than the last. Many other forms such as when and unless have implicit progn, so you can do something like: (unless (zerop index) (first-form) (second-form)) If you want to learn the basics of Common Lisp, I suggest you read Practical Common Lisp by Peter Seibel. It's free online.

 Related Discussions Atomic, Solid State, Comp. Physics 0 Programming & Computer Science 1 Programming & Computer Science 11 Computing & Technology 4 Computing & Technology 1