scott.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. (define lessons
  2. '(
  3. ;; lesson 0
  4. "Hello! I'm Professor Scott.
  5. You must want me to help you learn Scheme.
  6. So let's go to the first lesson. Say:
  7. > (next-lesson! prof-scott)
  8. "
  9. ;; lesson 1
  10. "Cool ! (I like to say Cooool :) ).
  11. You've just executed a Scheme expression.
  12. More precisely, you evaluated the procedure next-lesson! with the argument prof-scott (it's me !).
  13. Note you can run this tutorial again by evaluating:
  14. > (repeat-lesson! prof-scott)
  15. To return to the previous lesson, evaluate:
  16. > (previous-lesson! prof-scott)
  17. Try to evaluate this expression:
  18. > (display \"hello world!\\n\")
  19. Then go to the next lesson:
  20. > (next-lesson! prof-scott)
  21. "
  22. ;; lesson 2
  23. "You now know how to execute Scheme code.
  24. Now let's talk about basic data types.
  25. 1, 2, 100, 2/3 ... are numbers, and have many procedures that operate on them.
  26. Evaluate these ones:
  27. > 2
  28. > 1/3
  29. > (+ 1/3 4/5)
  30. > (round 18/5)
  31. > (- 1)
  32. > (- (- 1))
  33. > (odd? (+ 1 3))
  34. When you are done, say:
  35. > (next-lesson! prof-scott)
  36. "
  37. "A character is represented by a #\\ before the character itself.
  38. > #\\A
  39. > (char->integer #\\B)
  40. > #\\return
  41. > #\\space
  42. > (next-lesson! prof-scott)
  43. "
  44. "A string is a collection of characters. Use double quotes to enclose a string. Print these expressions:
  45. > \"prof-scott\"
  46. > (string-length \"prof-scott\")
  47. > (string-upcase \"abc\")
  48. > (list->string (reverse (string->list \"Hello World\")))
  49. You can access each character using the string-ref procedure.
  50. > (string-ref \"prof-scott\" 0)
  51. String concatenation uses the string-append procedure:
  52. > (string-append \"prof-scott\" \" is cool\")
  53. > (next-lesson! prof-scott)
  54. "
  55. "A symbol is a string which is guaranteed to be globally unique.
  56. There is one and only one symbol 'prof-scott. There may be several \"prof-scott\" strings.
  57. eq? returns true only if the two arguments are the same.
  58. > (string->symbol \"prof-scott\")
  59. > (symbol->string 'prof-scott)
  60. > (eq? (number->string 2) (number->string 2))
  61. > (eq? (string->symbol (number->string 2)) (string->symbol (number->string 2)))
  62. > (next-lesson! prof-scott)
  63. "
  64. "Literal lists can be created by quoting them:
  65. > '(1 2 3)
  66. > (length '(1 2 3 (4 5 6)))
  67. > (null? '(1 2 4))
  68. > (list-ref '(1 2 3) 0)
  69. > (next-lesson! prof-scott)
  70. "
  71. "Lists can also be created dynamically using the list procedure:
  72. > (list (+ 2 3) (* 6 6))
  73. > (length (list (+ 2 3) (* 6 6) (string-append \"hello\" \" Scott\")))
  74. > (next-lesson! (list-ref (list prof-scott) 0))
  75. "
  76. "Mathematical operations are just ordinary procedures in Scheme.
  77. The operator comes before the arguments in the expression, not between them.
  78. This means that order of operations is simply the arguments given to each procedure.
  79. > (* (+ 2 2) 10)
  80. > (+ 2 (* 2 10))
  81. > (/ (- 8 5) 2)
  82. > (- 8 (/ 5 2))
  83. > (next-lesson! prof-scott)
  84. "
  85. "Math is cool! Let's talk about lambdas.
  86. Lambdas are anonymous procedures that can be stored into variables and executed on demand.
  87. Lambdas have a special syntax:
  88. > (lambda () (display \"Hello\"))
  89. does not display anything because the body of the lambda is not executed.
  90. Here is a lambda that adds 2 to its argument (its argument is named x):
  91. > (lambda (x) (+ x 2))
  92. We can execute a lambda by using it like any other procedure:
  93. > ((lambda (x) (+ x 2)) 5)
  94. > ((lambda () (display \"Hello\")))
  95. > ((lambda (x) (+ x 2)) 10)
  96. > ((lambda (x y) (+ x y)) 3 5)
  97. > ((lambda () (next-lesson! prof-scott)))
  98. "
  99. "Lambdas can be assigned to a variable then executed later.
  100. > (define b (lambda (x) (+ x 2)))
  101. > (b 12)
  102. There is also a shorthand that means the same thing:
  103. > (define (b2 x) (+ x 2))
  104. > (b2 12)
  105. > (next-lesson! prof-scott)
  106. "
  107. "Conditionals follow the form (if predicate? consequent alternative).
  108. > (if (< 1 2) 100 42)
  109. Conditionals without an alternative path, or with multi-line consequents, should use cond.
  110. > (cond ((> 3 10) (display \"Maybe there\\\"s a bug...\")) (else (display \"No: 3 is less than 10\")))
  111. > (cond ((= 3 3) (next-lesson! prof-scott)))
  112. "
  113. "In order to execute a loop, it is necessary to define a named lambda.
  114. > (let p ((i 1)) (when (<= i 100) (display i) (newline) (p (+ i 1))))
  115. > (let p ((i 1)) (when (<= i 100) (display i) (newline) (p (+ i 3))))
  116. > (let p ((i 100)) (when (>= i 0) (display i) (newline) (p (- i 2))))
  117. > (next-lesson! prof-scott)
  118. "
  119. "There are some higher-order procedures that take a procedure and a list as its arguments.
  120. > (for-each (lambda (n) (display n) (newline)) '(11 38 3 -2 10))
  121. > (map abs '(11 38 3 -2 10))
  122. > (map odd? '(11 38 3 -2 10))
  123. > (for-each next-lesson! (list prof-scott))
  124. "
  125. "This tutorial is done. Enjoy programming with Scheme.
  126. See you soon!
  127. "
  128. ))
  129. (define prof-scott
  130. (let ((current-lesson -1))
  131. (define (display-lesson n)
  132. (define max-lesson (length lessons))
  133. (define as-string (string-append "("
  134. (number->string (+ 1 n))
  135. "/"
  136. (number->string max-lesson)
  137. ")"))
  138. (display as-string)
  139. (newline)
  140. (newline)
  141. (display (list-ref lessons n)))
  142. (lambda (msg self . rest)
  143. (case msg
  144. ((previous-lesson!)
  145. (cond
  146. ((<= current-lesson 0)
  147. (display "There are no previous lessons.\n"))
  148. (else
  149. (set! current-lesson (- current-lesson 1))
  150. (display-lesson current-lesson))))
  151. ((repeat-lesson!)
  152. (display-lesson current-lesson))
  153. ((next-lesson!)
  154. (cond
  155. ((>= current-lesson (- (length lessons) 1))
  156. (display "There are no further lessons.\n"))
  157. (else
  158. (set! current-lesson (+ 1 current-lesson))
  159. (display-lesson current-lesson))))))))
  160. (define (next-lesson! obj)
  161. (obj 'next-lesson! obj))
  162. (define (repeat-lesson! obj)
  163. (obj 'repeat-lesson! obj))
  164. (define (previous-lesson! obj)
  165. (obj 'previous-lesson! obj))
  166. (define (main)
  167. (next-lesson! prof-scott))