beginner.txt 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. MASSACHVSETTS INSTITVTE OF TECHNOLOGY
  2. Classical Mechanics, A Computational Approach
  3. GETTING STARTED
  4. The purpose of this document is to introduce a beginner to the use of
  5. the Scheme Mechanics system. This is not intended to be an
  6. introduction to the language Scheme or the notation used in the
  7. mechanics, or a catalog of the capabilities of the mechanics system.
  8. The following instructions assume that you are using an X window
  9. system on a Unix/GNU-Linux substrate.
  10. To Start The System
  11. On the MIT Athena system to get access to the system
  12. get an xterm shell and type
  13. add scmutils<Enter>
  14. To start up the system type
  15. mechanics&<Enter>
  16. at the Xterm prompt. The "<Enter>" appearing in the displayed line
  17. above stands for the carriage return (or enter key) on your keyboard.
  18. The ampersand "&" allows the shell to accept further commands even as
  19. the mechanics system is in use.
  20. This will pop up two windows. One of these (the edwin-xterm window)
  21. may be minimized and you should not interact with it. The other
  22. window (Edwin) will be opened and you will interact with that window.
  23. Edwin
  24. Edwin is the Emacs-compatible editor, written in Scheme, that provides
  25. the primary user interface to the mechanics system.
  26. Edwin commands use control and meta characters. Control and meta are
  27. modifiers like case shift. They are invoked by holding a modifier key
  28. depressed while typing the key to be modified. For example, the
  29. character described as C-x is typed by holding down the "control" (or
  30. "Ctrl") key while typing the "x" key. Meta is usually mapped to the
  31. key or keys labeled "alt". Characters using the meta modifier are
  32. denoted with "M-" as in "M-f". It is common to use several modifiers
  33. simultaneously. For example, you might have a C-M-f command that is
  34. invoked by holding down both the control and the meta modifiers while
  35. typing an "f".
  36. This document will not attempt to teach you Emacs -- you should use
  37. the Emacs tutorial that is supplied with the system to learn to use
  38. the editor effectively. It takes about 1 hour and is well worth the
  39. effort. The tutorial also explains how to evaluate scheme
  40. expressions. In the following we will assume you have gone through
  41. the tutorial. To enter the tutorial type "C-h t" at the edwin
  42. window. (That's the character "C-h" followed by the character "t".)
  43. To Exit The System
  44. To exit the mechanics system, and close the editor window, type "C-x
  45. C-c" at the Edwin window. (That's "C-x" followed by "C-c".) Edwin may
  46. ask you if you want to write out any files that you may have modified,
  47. and then it will exit, closing the Scheme system.
  48. File Naming Conventions
  49. Edwin buffers (you will learn about them in the tutorial) have modes
  50. that determine the meanings of commands executed in them. For
  51. example, it is convenient when editing Scheme programs to have a
  52. variety of commands for balancing parentheses and for preparing
  53. programs with nice indentation. These commands would not make sense
  54. for the language C, and C editing conventions would not be very good
  55. for Scheme or text. If we follow certain conventions for naming files
  56. then the appropriate command set will be used when we edit the
  57. contents of those files. The file extension used for Scheme files is
  58. ".scm", for C it is ".c" and for text it is ".txt". For instance, the
  59. file "homework-1.scm" will be edited in scheme mode. Some commands in
  60. Scheme mode allow you to evaluate expressions, with the results
  61. appearing in the *scheme* buffer.
  62. Working Conventions
  63. We find it useful to construct code for a particular problem in an
  64. appropriately named file. For example, if we are writing code to
  65. study the driven pendulum we might put the code in a file called
  66. "driven-pendulum.scm". As the code is developed, expressions can be
  67. evaluated and the results appear in the *scheme* buffer. We often
  68. find it convenient to divide the edwin window and display both the
  69. code file buffer and the *scheme* buffer at the same time.
  70. Be sure to save any changes you have made before exiting the system.
  71. To save your changes to a file put the cursor in the buffer associated
  72. with the file and type "C-x C-s".
  73. There are several ways of putting comments into a scheme file. A
  74. region can be commented by putting "#|" at the beginning of the region
  75. to be commented and "|#" at the end of the commented region. For
  76. example,
  77. #|
  78. this is a commented region,
  79. that can extend over multiple lines
  80. |#
  81. Comments within a line are started with a semicolon ";". For example,
  82. (+ 2 2) ; same as 4
  83. Comments at the begining of a line conventionally begin with ";;;".
  84. For example,
  85. ;;; this is code for the driven pendulum
  86. An advantage of constructing the code in a separate file is that it
  87. can be reloaded (reevaluating each of the expressions in the file) in
  88. another session. To load the scheme file "double-pendulum.scm"
  89. evaluate the scheme expression: (load "double-pendulum.scm")
  90. One can also evaluate all the expressions in a file by loading the
  91. file into an edwin buffer and typing M-o at the buffer.
  92. It is important to comment out all regions of a file that you do not
  93. want evaluated if the file is to be loaded or if you want to be able
  94. to execute a M-o command in the associated buffer. So for example, we
  95. often write code, consisting of definitions of procedures and data.
  96. We annotate the code with the results of evaluating expressions, to
  97. help us remember how the procedures are to be called or how they
  98. work. The regions of the file that are the annotations are set off as
  99. comments. Thus the definitions can be loaded, and we can execute the
  100. demonstration examples only if we want to. For example, the following
  101. might appear in a file named "fibonacci.scm"
  102. ;;;----------------------------------------------------------------
  103. ;;; Fibonacci numbers
  104. (define (fib n)
  105. (if (< n 2)
  106. n
  107. (+ (fib (- n 1))
  108. (fib (- n 2)))))
  109. #|
  110. ;;; Examples of use
  111. (fib 20)
  112. ;Value: 6765
  113. (iota 5)
  114. ;Value 150: (0 1 2 3 4)
  115. (for-each (compose write-line fib)
  116. (iota 10))
  117. 0
  118. 1
  119. 1
  120. 2
  121. 3
  122. 5
  123. 8
  124. 13
  125. 21
  126. 34
  127. ;Unspecified return value
  128. |#
  129. ;;; end of fibonacci.scm
  130. ;;;----------------------------------------------------------------
  131. Handling Errors
  132. You will often evaluate an improperly formed expression or an
  133. expression which has no sensible value such as "(/ 1 0)". This will
  134. cause the system to enter an error state and ask you if you want to
  135. start the debugger. If you do not want to enter the debugger answer
  136. "n".
  137. After examining an error, whether or not you choose to enter the
  138. debugger, you should type "C-c C-c" to get back to the top level.
  139. This is important because if you don't the evaluations you
  140. subsequently perform may refer to the error state rather than to the
  141. state you intended.
  142. Documentation
  143. There are important sources of documentation that you should know
  144. about. The Scheme system is extensively documented using the Emacs
  145. info system. To get to the info system, type "C-h i".
  146. If at any time in a Scheme-mode buffer you want to know the possible
  147. completions of a symbol, just type "C-M-i" (or M-<tab>). This will
  148. pop up an Edwin window that shows all symbols known by the system
  149. beginning with your initial segment.
  150. You may also want to know the arguments to a procedure. If the cursor
  151. is after the procedure name in an expression, you can type "M-A"
  152. (meta-shift-a) and a description of the arguments will appear in the
  153. Edwin command-line minibuffer (at the bottom of your Edwin window).
  154. If you want to see the definition of any procedure (including any
  155. non-primitive system procedure) you may use the pretty printer (the pp
  156. procedure). For example:
  157. (pp fib)
  158. (named-lambda (fib n)
  159. (if (< n 2)
  160. n
  161. (+ (fib (- n 1)) (fib (- n 2)))))
  162. (pp Lagrangian->Hamiltonian)
  163. (named-lambda (Lagrangian->Hamiltonian-procedure the-Lagrangian)
  164. (lambda (H-state)
  165. (let ((t (time H-state))
  166. (q (coordinate H-state))
  167. (p (momentum H-state)))
  168. (define (L qdot)
  169. (the-Lagrangian (up t q qdot)))
  170. ((Legendre-transform-procedure L) p))))
  171. The prinout from pp may not be exactly what you typed in, because the
  172. internal representation of your procedure may be "compiled".
  173. You should not confuse the pretty printer (pp) with the
  174. print-expression (pe) and show-expression (se) procedures used to
  175. display the results of algebraic manipulation. These procedures do
  176. much more than print in a nicely-formatted way. They do algebraic
  177. simplification and pp or TeX the result.
  178. You may want to recover the TeX produced by show-expression to
  179. include in a TeX document describing your results. To get this
  180. evaluate (display last-tex-string-generated).
  181. You should not be afraid to try the debugging system. All parts of
  182. the system are self documenting. So if you enter the debugger you
  183. will see that the first line in the debugging window tells how to get
  184. out of the debugger and how to get more information about how to use
  185. it. Indeed, the *scheme* buffer itself can give you information about
  186. the Edwin commands that are relevant to it, using the command "C-h m".