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