Page 67 - DCAP310_INTRODUCTION_TO_ARTIFICIAL_INTELLIGENCE_AND_EXPERT_SYSTEMS
P. 67
Unit 4: LISP
Caddr Notes
This returns the third element of a list. (The z coordinate)
(setq c (caddr pt1))
Would return:
(0.0)
AutoLisp has other functions that will retrieve values from lists of more than three elements.
(Caar, cadar, etc.). You can, though, us another function to access any element of a list. This is the
“nth” function.
nth
The syntax for the nth function is as follows:
(nth num list)
“num” is the number of the element to return. Just remember that zero is the first element. For
example given the list:
(setq d ‘(“M10” “M20” “M30” 10.25))
(setq e (nth 0 d))
Would return:
(“M10”)
And likewise:
(setq f (nth 3 d))
Would return:
(10.25)
Syntax Synergy
The thing that I think scares most everyday hackers away from lisp is the (comparatively) very
odd syntax it’s written in. One of the simple functions we wrote while working through the first
couple units looks like this:
(defun list-copy (lst)
(if (atom lst)
lst
(cons (car lst) (list-copy (cdr lst)))))
To someone who’s used to the simula syntax languages (as most programmers are), this can be
a very challenging paradigm shift. All those parentheses hovering around, seemingly providing
random groupings of keywords. It’s like this for a pragmatic reason, though, and in the end it’s
actually simpler than the languages you might be used to. You don’t have to know where to use
braces, brackets, parentheses, or dots.
Notes Everything is grouped by the same syntax, and everything is a list. It takes some
getting used to, but if you work with it for a couple days your brain adapts and starts to
actually compose your code ideas this way.
LOVELY PROFESSIONAL UNIVERSITY 61