home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / cad_util / alsps.zip / ALSP2.DOC < prev    next >
Text File  |  1993-11-04  |  8KB  |  256 lines

  1. This is Lesson 2 of a series of AutoLISP training exercises given
  2. on the CompuServe ADESK Forum by the Autodesk, Inc. Training
  3. Department.
  4.  
  5. EVERY OBJECT HAS A VALUE
  6.  
  7. There are two types of objects in AutoLISP; atoms and lists.
  8. Every atom and every list has a value.  The process by which
  9. AutoLISP determines the value of an object is called
  10. "evaluation".
  11.  
  12. EVALUATION OF ATOMS
  13.  
  14. Atoms are simple objects, and they are evaluated using simple
  15. rules according to their data type.  There are other data types
  16. apart from those listed below; they will be examined in detail in
  17. the next lesson.  The value of a symbol may not be obvious until
  18. the process of "binding" is discussed along with the evaluation
  19. of lists.
  20.  
  21. Type of Atom        Example   Evaluation rule     Value
  22.  
  23. Integer             1         Value is itself     1
  24. Real                4.5       Value is itself     4.5
  25. String              "text"    Value is itself     "text"
  26. Symbol              X         Current binding     Whatever was
  27.                                                   last assigned
  28.  
  29. EVALUATION OF LISTS
  30.  
  31. The value of a list can generally be determined in one of two
  32. ways; either by taking the list at face value, or by evaluating
  33. the list.  One method will generally yield different results, or
  34. a different value, than the other.
  35.  
  36. If a list is taken at face value, then its value is itself.  This
  37. will become somewhat clearer upon explanation of the (quote)
  38. function later in this lesson.
  39.  
  40. Lists are evaluated according to the first element of the list.
  41. If the first element is the name of an internal AutoLISP
  42. function, or subr, the remaining list elements are passed to the
  43. subr as the formal arguments and are evaluated by the subr.
  44.  
  45. For example, this is a list of three elements, the subr + and the
  46. integers 1 and 2.
  47.  
  48. (+ 1 2)
  49.  
  50. This is the evaluation process by which AutoLISP determines the
  51. value of this list.  All evaluations of the same list occur in
  52. exactly the same manner, every time.  The same general process of
  53. evaluation is applied to every list that can be and is evaluated
  54. in exactly the same manner, every time.
  55.  
  56. (+ ..
  57.  
  58. AutoLISP looks at the first element of the list, where it expects
  59. to find a function.  Functions are either internal AutoLISP
  60. functions, or subrs, such as those listed in Chapter 4, "AutoLISP
  61. Functions", of the APR, or user-defined functions created by the
  62. (defun) or (lambda) functions.
  63.  
  64. AutoLISP evaluates the function, or first element of the list.  A
  65. function evaluates to a set of instructions that tells AutoLISP
  66. what to do next.
  67.  
  68. (Isn't that elegant?  Every complex expression in AutoLISP
  69. carries its own processing instructions with it, in exactly the
  70. same place every time; at the head of the list.)
  71.  
  72. In this case the function + evaluates to a set of instructions
  73. that tells AutoLISP, "Go find the value of every other element in
  74. this list.  When you're done, add all the values together and
  75. return the result as the value of the entire list."
  76.  
  77. (.. 1 ..)
  78.  
  79. Following its instructions, AutoLISP finds the value of the next
  80. element in the list, the atom and integer 1.  Integers evaluate
  81. to themselves, so AutoLISP stores the value 1 and continues with
  82. its instructions.
  83.  
  84. (.. .. 2)
  85.  
  86. The value of the integer 2 is 2.  AutoLISP stores this value and
  87. continues.
  88.  
  89. Elements in a list following a function are arguments to the
  90. function; that is, they are the data upon which the function
  91. operates.  Finding no more elements in the list, or arguments to
  92. the function, AutoLISP finishes the instructions it received by
  93. evaluating the function and adds the values of the arguments,
  94. returning the integer 3 as the value of the list.
  95.  
  96. Command:  (+ 1 2)
  97. 3
  98.  
  99. A MORE COMPLEX EXAMPLE
  100.  
  101. AutoLISP (any Lisp, in fact) always evaluates the elements in a
  102. list from left to right.  You may have read that elements are
  103. evaluated in a nested manner, from the inside out; this is wrong.
  104. It may be convenient for us in some cases to follow the
  105. evaluation process by working from the inside out, but that is
  106. never the way AutoLISP evaluates a list.  It always works from
  107. left to right.
  108.  
  109. Consider this list of three elements, the function (or subr) +,
  110. the atom 1, and the list (+ 2 3).
  111.  
  112. (+ 1 (+ 2 3))
  113.  
  114. This is how AutoLISP evaluates this list.
  115.  
  116. (+ ..
  117.  
  118. AutoLISP looks at the first element of the list and evaluates it.
  119. Since it's a function, it returns as its value a set of
  120. instructions on what AutoLISP should do next.
  121.  
  122. (.. 1 ..
  123.  
  124. Following the instructions of the + function, AutoLISP finds the
  125. value of the second element in the list (the first argument to
  126. the + function), the atom 1, stores that value, and continues.
  127.  
  128. (.. .. (+ 2 3)
  129.  
  130. AutoLISP finds the value of the third element in the list, the
  131. second argument to the + function.  The third element is itself a
  132. list.  How does AutoLISP evaluate a list?
  133.  
  134. First, it evaluates the function + and receives its instructions.
  135. Next, it finds the values of the arguments, adds them together,
  136. and returns the result, which in this case is the integer 5.
  137.  
  138. Finding no more elements in the list, AutoLISP finishes its
  139. instructions by adding the value of the two elements together and
  140. returning the value of the integer 6.
  141.  
  142. (+ 1 (+ 2 3))
  143.    |  \   /
  144.    |   \ /
  145.    |    |------------ Value of object is integer 5
  146.    |----------------- Value of object is integer 1
  147.  
  148. TAKING A LIST AT FACE VALUE
  149.  
  150. The function (quote) may be used to return a list (or an atom)
  151. unevaluated; that is, at face value.  (quote) takes a single
  152. argument, and returns as its value the argument unevaluated.
  153.  
  154. For example, this expression returns the list (1.0 2.0 3.0) at
  155. face value.
  156.  
  157. Command: (quote (1.0 2.0 3.0))
  158. (1.0 2.0 3.0)
  159.  
  160. What would happen if AutoLISP were asked to evaluate the list?
  161. Why?
  162.  
  163. Command: (1.0 2.0 3.0)
  164. ?
  165.  
  166. BINDING A SYMBOL TO A VALUE
  167.  
  168. Binding is the process by which a value is assigned to a
  169. user-defined symbol, or variable.  In AutoLISP, we say that a
  170. symbol (or variable) is bound to a value.  This is comparable to
  171. saying in the language BASIC that a value is equal to a variable.
  172.  
  173. The function (setq) is generally used to bind a symbol (or
  174. variable) to a value.  For example, this expression binds the
  175. symbol X to a value of 4.5.
  176.  
  177. Command: (setq x 4.5)
  178. 4.5
  179.  
  180. Command: !x
  181. 4.5
  182.  
  183. In BASIC, the same operation might be expressed as:
  184.  
  185. LET X = 4.5
  186.  
  187. The value of the subr (setq) is a set of instructions that boils
  188. down to this:
  189.  
  190. Find the value of the second argument to setq (the third element
  191. in the list).  Take the first argument as is; don't evaluate it.
  192. Bind the first argument to the value of the second argument.
  193.  
  194. EXERCISE
  195.  
  196. Which of these lists can legally be evaluated and which must be
  197. taken at face value?
  198.  
  199. 1.        (+ 1 2)
  200. 2.        (+ 1 (+ 2 3))
  201. 3.        (1.0 2.0 3.0)
  202. 4.        (quote (1.0 2.0 3.0))
  203. 5.        (setq x 4.5)
  204. 6.        (setq y (1.0 2.0 3.0))
  205.  
  206. Using the functions +, -, /, and * as documented in the APR,
  207. convert the following algebraic expressions into AutoLISP
  208. expressions.  For example, the expression:
  209.  
  210. 1 + 2
  211.  
  212. would be converted into the AutoLISP expression:
  213.  
  214. (+ 1 2)
  215.  
  216. 1.        3 + 10 + 5
  217. 2.        20 * 15
  218. 3.        16 - 10
  219. 4.        15 / 3
  220. 5.        5 + (10 * 2)
  221. 6.        (5 + 10) * 2
  222.  
  223. Using the (setq) function and the (quote) function (as needed),
  224. bind the variable X to the following values.  Check the value of
  225. the variable by using ! to find its current binding.
  226.  
  227. 1.        1
  228. 2.        4.5
  229. 3.        "text"
  230. 4.        (1.0 2.0 3.0)
  231.  
  232. Next week: Data Types
  233.  
  234. Answers to Lesson 1 Exercises:
  235.  
  236. 1.  atom
  237. 2.  atom
  238. 3.  list
  239. 4.  atom
  240. 5.  list
  241. 6.  atom and list
  242.  
  243. 1.  4.5
  244. 2.  "text"
  245. 3.  17
  246. 4.  <Subr: xxxx>
  247. 5.  nil
  248. 6.  nil
  249.  
  250. 1.  3
  251. 2.  3
  252. 3.  3
  253. 4.  5
  254. 5.  4
  255. 6.  none
  256.