How to Apply Cond?

How to Apply Cond?

CodeDing
Advisor Advisor
1,243 Views
21 Replies
Message 1 of 22

How to Apply Cond?

CodeDing
Advisor
Advisor

Can someone explain to me why this works:

(cond (nil) (1) ("a"))

 

But this does not work (nor simple variations using 'quote):

(apply 'cond (mapcar 'list '(nil "1" "a")))

 

I'm sure it has something to do with symbols & evaluations, etc. But I can't quite grasp it.

Is there a way that I can apply the COND function to an unknown list size?

 

Best,

0 Likes
1,244 Views
21 Replies
Replies (21)
Message 21 of 22

hak_vz
Advisor
Advisor

@CodeDing wrote:

Been trying to review this posting for some further understanding. I know it's not Autolisp, but should be same principle.

http://www.reduce-algebra.com/lisp-docs/allman1se29.html 


DD, you may also try Maxima.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 22 of 22

Kent1Cooper
Consultant
Consultant

@CodeDing wrote:

....

Is there a way that I can apply the COND function to an unknown list size?

...,


To that core question of how to use (cond) over a list of conditions of unknown length, which seems to be the goal, maybe the way to go is not to use (cond) itself if it's making itself difficult, but to simulate what (cond) does.

 

Taking your second function from Message 1, and putting the result into a thelist variable:
(setq thelist (mapcar 'list '(nil "1" "a")))
returns:
((nil) ("1") ("a"))

 

Then if we apply this [lightly tested]:

(setq done nil)
(while (and thelist (not done)); still something left, hasn't found non-nil test yet
  (if (eval (caar thelist)); first item's first item is not, or does not evaluate to, nil
    (progn ; then -- go with it
      (setq done T); to stop (while) loop
      (if (= (length (car thelist)) 1); no "result" code following test expression
        (eval (caar thelist)); then -- return the item or what it evaluates to
        (eval (cdr (eval (caar thelist)))); else -- run what follows test expression
      ); if
    ); progn
    (setq thelist (cdr thelist)); else [nil] -- take first item off, check next
  ); if
); while

it returns "1" [for me], the first non-nil item in the list, as you would expect of (cond).

If we use your other case:

(setq thelist (mapcar 'list '(T "1" "a")))

it returns T.

 

I tried making it more complicated, such as to define something with a test expression and something arbitrary to do if it's non-nil [as one would expect to encounter within a (cond) function]:

(setq test '("1" (command "_.zoom" "_extents") (setvar 'cecolor "2")))

Then I put that test variable into thelist:

(setq thelist (mapcar 'list (list what test "1" "a")))
The what is a non-existent variable, for trial purpose.  When I run the code above, it bypasses what for returning nil, and runs what's in the TEST after its initial test expression that is not nil, that is, it does the ZOOM command and System Variable setting called for in it.

Kent Cooper, AIA
0 Likes