home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lisp / interpre / apteryx / tutor0.lsp < prev    next >
Lisp/Scheme  |  1994-03-18  |  3KB  |  87 lines

  1. ; Copyright 1994 Apteryx Lisp Ltd
  2.  
  3. ; Use the mouse to move the cursor to the next line
  4. ; and press F4 to evaluate it. See the result in the
  5. ; LispOutput window
  6. ; (To get the best view, maximize the main window, select 
  7. ; this window and choose the Window:TileWithOutput menu option.)
  8.  
  9. (+ 2 3)
  10.  
  11. ; Congratulations, you've just executed your first lisp program !
  12. ; Can you see what it did ?        (Answer: 2 + 3 = 5)
  13.  
  14. ; Here's another
  15.  
  16. (+ 2 (* 4 10))
  17.  
  18. ; * means multiply
  19.  
  20. ; (+ 2 3) is a list with 3 members - the symbol + and the numbers 2
  21. ; and 3.
  22. ; When you press F4 you evaluate the list. + is the name of a function
  23. ; and 2 and 3 are the arguments. + is applied to its arguments and
  24. ; returns a result, which for + is the sum of the arguments.
  25.  
  26. ; Similiarly (* 4 10) when evaluated returns the result of multiplying
  27. ; 4 times 10. (To see this, select the expression using the mouse
  28. ; and then press F4.)
  29.  
  30. ; In (+ 2 (* 4 10)) the second argument to the + is the list (* 4 10).
  31. ; This list argument is evaluated first, to give 40, and this result
  32. ; is then passed as the actual second argument to the function + which
  33. ; returns the result 42.
  34.  
  35. ; Here's a program that counts the number of elements in a list -
  36.  
  37. (length '(a b c d e))
  38.  
  39. ; length is a function which returns the number of elements in a list.
  40. ; The one and only argument to length is '(a b c d e). What the ' quote
  41. ; character means is don't try to evaluate this list (as was done with
  42. ; (* 4 10) in the example above), but pass it unchanged to the function.
  43. ; '(a b c d e) is an abbreviation for (quote (a b c d e)). 
  44. ; The members of (a b c d e) are a, b, c, d and e which are all symbols.
  45. ; ' is needed before list or symbol arguments that aren't meant to be
  46. ; evaluated - its optional but unnecessary before other types of argument
  47. ; For example - 
  48.  
  49. (+ '2 '3)
  50.  
  51. ; or
  52.  
  53. (+ (quote 2) (quote 3))
  54.  
  55. ; cons is a function that puts a new element onto the beginning of 
  56. ; a list.
  57. ; Evaluate the following - 
  58.  
  59. (cons 'a '(b c d e))
  60.  
  61. ; And now make your own function - here's a function that multiplies
  62. ; a number by itself
  63.  
  64. (defun square (x) (* x x)) ; F4 to make the function
  65.  
  66. (square 5)   ; F4 here to try it out
  67.  
  68. ; looking at (defun square (x) (* x x))
  69.  
  70. ; defun is the special form (not a function as such because it doesn't
  71. ; evaluate its arguments)
  72. ; square is the name of the new function
  73. ; (x) is the argument list - the symbol x represents the one and only
  74. ; argument that the function is going to take
  75. ; (* x x) is the expression that is evaluated to give the final result
  76. ; In the expression (* x x) the x's are evaluated to the value of the
  77. ; argument x passed in when square is actually used
  78. ; i.e. in (square 5) the x's evaluate to 5, and (* 5 5) is evaluated
  79. ; to give the final result.
  80.  
  81. (mapcar #'square '(1 2 3 4 5))
  82.  
  83. ; mapcar applies its first argument to each member of the list passed
  84. ; as second argument, and returns a list made from the results.
  85. ; ( #'square means the function referred to by the symbol square. )
  86.  
  87.