Executing multiple statements with if/cond (Lisp)

  • Thread starter Thread starter kataya
  • Start date Start date
  • Tags Tags
    Multiple
AI Thread Summary
The discussion centers on executing multiple statements based on conditions in Lisp, highlighting the differences from other programming languages. The original poster encounters a compiler error due to incorrect function calls and misunderstandings about how Lisp expressions work, particularly regarding side effects. It is clarified that Lisp does not modify variables directly; instead, it evaluates expressions and returns values. The recommended solution involves using "let" statements to define new variables within a scope, allowing for sequential evaluations. The use of "progn" is also suggested for executing multiple forms in order, as it evaluates all contained forms and returns the value of the last one. Other forms like "when" and "unless" are mentioned for their implicit use of "progn." For further learning, "Practical Common Lisp" by Peter Seibel is recommended as a resource.
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:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
Back
Top