@Anonymous wrote:
Does this condn make any difference to the functionality of the lisp ??
Can u tell me which is better...using ifs or condn.....
No, it wouldn't make any difference to the functionality.
Which is better depends on circumstances. The (if) function takes a 'then' expression [what to do if the test part returns something other than nil] and an 'else' expression [what to do if it returns nil, which you can omit if you don't want it to do anything]. So it's a two-situations-at-the-most operation, concerned only with whether a single test is satisfied. The (cond) function can look at a whole series of any number of possible situations, and they don't even need to be related tests, and will do whatever it's instructed for the first of those that is satisfied, and then stop.
@Anonymous's structure with the (if) functions nested inside each other takes some more code, but is at least better than what you sometimes see -- a series of independent (if) functions testing the same thing for possible values, with 'then' expressions but no 'else' expressions for each, such as:
(if (= hellow "L") (wall-l))
(if (= hellow "T") (wall-t))
(if (= hellow "X") (c:wall-x))
The drawback in that is that even if 'hellow' is set to "L", and the first (if) function finds that to be the case, and it does the (wall-l) thing in response, the routine still has to go on and test for each of the rest of the possible values, even though none of those tests will be satisfied. That's the big benefit of (cond), especially with a large number of possible conditions -- as soon as it finds a test satisfied, it acts on it and doesn't look at the rest of them at all. That's effectively what happens in @Anonymous's nested-(if)s approach -- when one is satisfied, it won't look at the rest -- but it's wordier than using (cond), and gets more so the more conditions are involved.
Just for fun, here's another way to go about it, after the 'hellow' variable has been set, not using either (if) or (cond):
(eval (cdr (assoc hellow '(("L" wall-l) ("T" wall-t) ("X" C:wall-x)))))
Kent Cooper, AIA