home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lisp / interpre / apteryx / tutor1.lsp < prev    next >
Text File  |  1994-02-09  |  8KB  |  164 lines

  1. ; Copyright 1993 Apteryx Lisp Ltd
  2.  
  3. ;;; Lesson 1
  4.  
  5. ; Welcome to Apteryx Lisp, the easy-to-use Lisp system for Windows (TM).
  6.  
  7. Lisp is a computer programming language, i.e. a language that tells a
  8. computer how to process information.
  9.  
  10. Information processing generally involves input information and output
  11. information. The input information is what we already know, the output
  12. information is something that we would like to know, which can be calculated
  13. in some way from the input information.
  14.  
  15. This general concept of information processing can be identified with the
  16. mathematical concept of a function. A function is something that takes an
  17. input argument or arguments (i.e. the input information) and returns a
  18. result (i.e. the output information).
  19.  
  20. To clarify the concept further we must know what information. We can define
  21. a piece of information to be a particular value that comes from a certain 
  22. set of values. For example a natural number is a particular value from the
  23. set of natural numbers i.e. {0, 1, 2, 3 ... etc.}. The set of possible values
  24. is sometimes called a data-type (particularly in computer science).
  25.  
  26. The data value might tell us something about the real world, for example a 
  27. number might be the number of apples in a barrel.
  28.  
  29. A simple and familiar example of a function is addition, which we can 
  30. represent with the symbol +. Its required input is two numbers, and the
  31. output returned is the sum of those two numbers.
  32.  
  33. The following is a Lisp program that adds two numbers -
  34.  
  35. (+ 23 145)
  36.  
  37. To execute, first choose Tile with Output from the Window menu so that
  38. you can see this window and the LispOutput window,
  39. then place the text cursor after the right bracket, and press the key F4
  40. (or choose the menu option Lisp:Eval). The result of executing this function
  41. should appear in the LispOutput window. If you like, try changing the 
  42. numbers and re-execute the function, seeing what result you get.
  43.  
  44. We can make the output of one program the input to another, simply by
  45. writing the first program where the required input for the second program
  46. goes. For example -
  47.  
  48. (* 10 (+ 23 145))
  49.  
  50. where * represents multiplication. Try evaluating this expression (using F4).
  51. The result of (+ 23 145) = 168 becomes one of the inputs to the function *,
  52. so that the final result returned is 168 * 10 = 1680.
  53.  
  54. One thing about Lisp that distinguishes it from virtually all other computer
  55. programming languages is that the programs themselves are data, i.e. values
  56. from a set of possible values. For example the expression (* 10 (+ 23 145))
  57. describes a list of three elements which are the symbol *, the number 10
  58. and (+ 23 145) which is itself a list of three elements - the symbol +,
  59. the number 23 and the number 145. (Because the numbers don't have decimal
  60. points they are regarded as integers, which are distinguished from floating
  61. point numbers.)
  62.  
  63. There are Lisp functions that take lists as arguments. To pass a list as
  64. an argument to a function we must put a quote character ' in front of it,
  65. so that it is not interpreted as a function. For example, length is the name
  66. of a function that takes a list as input and returns the number of elements
  67. in the list. Try executing the following -
  68.  
  69. (length '(Sam jim fred tom))
  70.  
  71. If the quote were left out, Lisp would first try to calculate the result of 
  72. (Sam jim fred tom) but would fail because there is no function called 
  73. Sam. The process of calculating the final result of an expression is
  74. called evaluation. We can say that the quote suppresses evaluation.
  75.  
  76. There is a function called eval which evaluates its argument. Here is an
  77. example which also uses a function called append, which sticks two or more
  78. lists together -
  79.  
  80. (eval (append '(+) '(4 5)))
  81.  
  82. The final result should be 9. What happens is that first the expression
  83. (append '(+) '(4 5)) returns the result (+ 4 5), and then the function
  84. eval evaluates this list to return the final result 9.
  85.  
  86. Apteryx Lisp has a facility that lets you evaluate partial expressions. Simply
  87. select the desired selection using the mouse, for example
  88.  (append '(+) '(4 5)) in the expression above, and then press F4. The 
  89. selected expression will then be evaluated. A failure to select the 
  90. sub-expression properly may result in an error of some sort. One way to 
  91. avoid this sort of error is to let Apteryx Lisp help you select the expression.
  92. For example, put the text cursor just before the second bracket, and
  93. press Shift-Ctrl right arrow, i.e. hold down the Shift and Ctrl keys and
  94. press the right arrow key. You should see the required sub-expression 
  95. selected. You can then press F4 to evaluate it.
  96.  
  97. ;;; Top-level expressions and indentation.
  98.  
  99. When you press F4 and there isn't a current text selection, Apteryx Lisp uses
  100. special rules to decide which is the expression to be executed. Basically,
  101. Apteryx Lisp looks for a top-level expression that the text cursor is in or 
  102. after. A top-level expression is one that is not contained in any other 
  103. expression. To avoid long searches up and down a file to determine if an
  104. expression is actually contained in a larger expression, Apteryx Lisp makes the
  105. assumption that an open bracket (or one of various other characters that
  106. can start an expression), which is hard up against the left margin, is
  107. the start of a top level expression. To be consistent with this convention,
  108. it is desirable that any sub-expression does not start at the beginning of
  109. a line. For example, the following expression takes up more than one line -
  110.  
  111. (+ 3
  112.   (* 5 6) )
  113.  
  114. We can place the cursor at the end of the expression, and press F2, which
  115. will select the whole expression.
  116.  
  117. We notice that the sub-expression (* 5 6) is indented. Indentation is 
  118. desirable because it helps us to read complex nested lists. Apteryx Lisp
  119. has various automatic indentation facilities that help us indent 
  120. expressions. These are -
  121.  
  122. Ctrl-Enter  Starts a new line that is correctly indented.
  123. Tab         Correctly indents the current line.
  124. F5          Corrects the indentation for the top-level expression selected
  125.             or implied by the text cursor.
  126.  
  127. It should be noted that these facilities use the same convention as F4
  128. and F2 to determine what is a top-level expression.
  129.  
  130. (+ 3 4
  131. (* 5 6) )
  132.  
  133. If we put the text cursor after the last bracket above, and press F5, to
  134. correct indentation, we actually get a syntax error, because Apteryx Lisp finds
  135. a non-indented open bracket at the beginning of (* 5 6), and parses
  136. forward, eventually finding an unbalanced right bracket at the end of the
  137. line, which causes the error. For the above example we can avoid the error
  138. by using Tab, which assumes that the current line might not be the
  139. start of a top-level expression, even if it is indented. If we've got
  140. a whole lot of unindented lines, we have to go to the first line and from
  141. there use F5.
  142.  
  143. (put cursor on this line
  144. (and
  145. (see
  146. (these lines
  147. (move left when you press F5) ) ) )
  148.  
  149. Apteryx Lisp will keep telling you that it's found a non-indented non-top-level
  150. expression, and you have to use Tab to indent each one. This seems a bit
  151. annoying, but avoids the problems caused by leaving out one right bracket,
  152. and having Apteryx Lisp interpret the whole of the rest of your program file
  153. as part of one expression (eventually failing when it gets to the bottom
  154. of the file still missing a required right bracket).
  155.  
  156. The best strategy is always use Ctrl-Enter to start a new line while typing
  157. Lisp expressions, and use Tab immediately if you forget to do so. F5 is 
  158. useful when you do something that changes the nested depth of part of
  159. an expression.
  160.  
  161.  
  162.  
  163.  
  164.