Executing multiple statements with if/cond (Lisp)

  • Thread starter kataya
  • Start date
  • Tags
    Multiple
In summary, the author is trying to write a function that will return the elements of an xlist up to a given index, but is having difficulty because the language does not have a built-in function or macro that can do this. He suggests using the progn function, which will evaluate all the contained forms in order and return the last value.
  • #1
kataya
23
0
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?
 
Last edited:
Technology news on Phys.org
  • #2
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'.)
 
  • #3
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:
Code:
(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:

Code:
(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.

:smile:
 
Last edited:

1. What is the purpose of using if/cond statements in Lisp?

The if/cond statement is used in Lisp to execute different sets of code based on different conditions. It allows for branching and decision-making within a program.

2. How do you write an if/cond statement in Lisp?

An if/cond statement in Lisp follows the syntax: (if condition then-statement else-statement) or (cond (condition1 then-statement1) (condition2 then-statement2) ... (t else-statement)) where the if statement checks a single condition and the cond statement checks multiple conditions.

3. Can you give an example of using multiple statements with if/cond in Lisp?

Yes, for example: (if (> x 10) (print "x is greater than 10") (print "x is less than or equal to 10")) or (cond ((= x 0) (print "x is equal to 0")) ((= x 1) (print "x is equal to 1")) (t (print "x is not equal to 0 or 1")))

4. What happens if none of the conditions in a cond statement are met?

If none of the conditions in a cond statement are met, then the t statement at the end will be executed. This is known as the "else" or default statement.

5. Are there any alternatives to using if/cond statements in Lisp?

Yes, there are other conditional statements in Lisp such as when and unless which only have a single condition and do not require an "else" statement. There is also the case statement for checking multiple conditions with a single value.

Similar threads

  • Programming and Computer Science
Replies
7
Views
8K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Linear and Abstract Algebra
Replies
15
Views
4K
  • Programming and Computer Science
Replies
31
Views
6K
  • Programming and Computer Science
Replies
1
Views
977
  • Programming and Computer Science
Replies
11
Views
14K
  • Programming and Computer Science
Replies
5
Views
1K
Replies
6
Views
1K
Back
Top