tags:

views:

146

answers:

3

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 statment I want to be able to execute (setq num NEWNUM) and (FUNCT2). One to set a new variable and then the other to call a function. Any ideas on how to do this?

+9  A: 

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
Chuck
+6  A: 

Also, when your if is 'one-armed' as they call it, it's typically also easier and more idiomatic to use when and unless: http://www.cs.cmu.edu/Groups/AI/html/hyperspec/HyperSpec/Body/mac_whencm_unless.html

Which means you get (when pred x y ... z) it will just evaluate x y z sequentially if pred is true. unless is used for the same if pred is NIL. x y z can contain from one to any. 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 now 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.

Lajla
+4  A: 

You can't use multiple statements with if, except with progn as posted above. But there is the cond form,

(cond
 ((find input num)     // if this 
  (setq num NEWNUM)    // then execute both of these
  (FUNCT2))

 (t
  (list 'not found)))  // else output this
JohnMaraist