Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you execute multiple statements in an "if" statement?

Tags:

lisp

This is my function:

(defun MyFunction(input)   (let ((NEWNUM (find input num)))     (if (find input num)              //if this        (setq num NEWNUM) (FUNCT2)      //then execute both of these     (list 'not found))))              //else output this 

So after the if statement I want to be able to execute (setq num NEWNUM) followed by (FUNCT2) in order to set a new variable and then call a function. Any ideas on how to do this?

like image 522
Bramble Avatar asked May 17 '10 19:05

Bramble


People also ask

How do you do multiple statements in an if statement?

If you want to execute multiple statements for the else condition, enclose the code in curly brackets. If you only need to execute a single statement for the else condition, you do not need to use curly brackets.

Can an IF condition have multiple statements?

The multiple IF conditions in Excel are IF statements contained within another IF statement. They are used to test multiple conditions simultaneously and return distinct values. The additional IF statements can be included in the “value if true” and “value if false” arguments of a standard IF formula.

Can you use 2 IF statements in Excel?

While Excel will allow you to nest up to 64 different IF functions, it's not at all advisable to do so.

Can you put multiple If statements in one cell?

As a worksheet function, the IF function can be entered as part of a formula in a cell of a worksheet. It is possible to nest multiple IF functions within one Excel formula. You can nest up to 7 IF functions to create a complex IF THEN ELSE statement.


2 Answers

To do several things in sequence, you want progn.

(defun MyFunction(input)   (let ((NEWNUM (find input num)))     (if (find input num)              //if this        (progn          (setq num NEWNUM)         (FUNCT2))      //then execute both of these     (list 'not found))))              //else output this 
like image 127
Chuck Avatar answered Oct 09 '22 22:10

Chuck


When your if is 'one-armed', as they call it (that is, it contains no else branch), it's typically easier and more idiomatic to use when and unless: http://www.cs.cmu.edu/Groups/AI/html/hyperspec/HyperSpec/Body/mac_whencm_unless.html

When you call (when pred x y ... z), it will just evaluate x y z sequentially if pred is true. unless behaves similarly when pred is NIL. x y z can represent any number of statements from one upwards. Thus:

(when pred (thunk)) 

is just the same as

(if pred (thunk)) 

Some people say when and unless should always be used for 'one-armed-ifs' because of clarity.

Edit: Your thread gave me an idea. This macro:

(defmacro if/seq (cond then else)   `(if ,cond (progn ,@then) (progn ,@else))) 

should enable this:

(if/seq (find input num)              //if this        ((setq num NEWNUM) (FUNCT2))      //then execute both of these     ((list 'not found)))))  

So the general format is:

(if/seq *condition* (x y ... z) (a b ... c)) 

Depending on the condition, it evaluates all of the subforms in the first or second, but only returns the last.

like image 45
Zorf Avatar answered Oct 09 '22 21:10

Zorf