Executing multiple statements with if/cond (Lisp)

  • Thread starter Thread starter kataya
  • Start date Start date
  • Tags Tags
    Multiple
Click For Summary
SUMMARY

This discussion addresses executing multiple statements conditionally in Lisp, specifically using Common Lisp. The user encountered issues with their function due to misunderstanding how expressions work in Lisp, particularly regarding side effects and variable binding. The solution involves using the let construct for variable binding and the progn function to evaluate multiple forms sequentially. The conversation also highlights the importance of understanding Lisp's evaluation model and suggests resources for further learning.

PREREQUISITES
  • Understanding of Common Lisp syntax and semantics
  • Familiarity with functional programming concepts
  • Knowledge of variable binding and scope in Lisp
  • Basic understanding of conditional expressions in programming
NEXT STEPS
  • Learn about the let construct in Common Lisp for variable binding
  • Explore the progn function for executing multiple forms
  • Read "Practical Common Lisp" by Peter Seibel for foundational knowledge
  • Investigate other conditional forms like when and unless in Lisp
USEFUL FOR

This discussion is beneficial for Lisp programmers, particularly those new to Common Lisp, as well as developers looking to deepen their understanding of conditional execution and variable management in functional programming.

kataya
Messages
22
Reaction score
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
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'.)
 
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:

Similar threads

Replies
7
Views
9K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
7K
Replies
11
Views
16K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
5K
  • · Replies 23 ·
Replies
23
Views
3K