tutorial.skb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ;;; Having fun with Guile: a tutorial
  2. ;;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
  3. ;;;
  4. ;;; This program is free software: you can redistribute it and/or
  5. ;;; modify it under the terms of the GNU Lesser General Public License
  6. ;;; as published by the Free Software Foundation, either version 3 of
  7. ;;; the License, or (at your option) any later version.
  8. ;;;
  9. ;;; This program is distributed in the hope that it will be useful,
  10. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;;; GNU Lesser General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU Lesser General Public License
  15. ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;;
  17. ;;; ... This tutorial is also optionally licensed, at your option,
  18. ;;; under the GNU Free Documentation License, Version 1.3
  19. ;;; or any later version published by the Free Software Foundation;
  20. ;;; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
  21. (use-modules (skribilo source lisp))
  22. (define doc-title [Having fun with Guile: a tutorial])
  23. (define repo-url [https://notabug.org/cwebber/guile-tutorial])
  24. (document
  25. :title doc-title
  26. :html-title doc-title
  27. ;;; Intro
  28. ;;; =====
  29. (chapter
  30. :title [Introductions]
  31. ;; TODO: Hello world image here
  32. (p [Hello!
  33. Welcome to
  34. ,(ref :url [https://www.gnu.org/software/guile/] :text [Guile]),
  35. and this little Guile tutorial!
  36. If you've been interested in learning Guile, but don't know how
  37. to dive in, this is the tutorial for you.
  38. We don't assume any prior programming experience in this tutorial,
  39. but if you do have prior experience, you'll find that will
  40. make things much easier.])
  41. (p [Guile is a couple of things: it's a language in the family of Scheme and
  42. Lisp, and it's also a language virtual machine on which many languages
  43. can run.
  44. The goal of this tutorial is to introduce you to the Scheme style
  45. language that Guile provides (or in other words, "Guile Scheme").])
  46. (p [There are many languages to choose from these days, but we hope you'll
  47. find that Guile has some special things to offer.
  48. Guile is "multi-paradigm", this means that if you can choose between
  49. imperative object-oriented style or pure functional programming style,
  50. or a variety of other techniques.
  51. (And if you don't know what that means, playing with Guile is a great
  52. way to learn!)
  53. Guile comes with many useful features built-in, and has a variety
  54. of libraries you can use to build anything from games to
  55. interactive websites.
  56. And if you've ever wanted to learn how programming languages work
  57. or ever wanted to tune them to your purposes, Guile is a great fit;
  58. you can easily peek inside to see what's going on and even easily
  59. add new language features.
  60. We think that's pretty neat!])
  61. (p [At its best, Guile is a playground full of wonderful things to explore.
  62. As such, many of the metaphors we use in this tutorial are based
  63. around playing with toys.
  64. We hope you learn a lot, and most importantly, have fun doing so.]))
  65. ;;; - Getting up and running
  66. ;;; (picture of one of those robots with a wind-up-toy-key on its back?)
  67. (chapter
  68. :title [Getting up and running]
  69. (include "up-and-running.skb"))
  70. ;;; Much like The Little Schemer uses food as variable names, I think
  71. ;;; it's a good idea to stick with abstract fun concepts. Here, I think
  72. ;;; it would be great to continue along with the "Guile is a playground,
  73. ;;; come play!" idea by using toys as variable names, and defining
  74. ;;; procedures that evoke nostalgia for older programmers and sound
  75. ;;; playful for younger ones.
  76. ;;;
  77. ;;; Some ideas:
  78. ;;; + could use building lists as putting toys in and out of a toy
  79. ;;; chest
  80. ;;;
  81. ;;; (define toy-chest '(robot teddy-bear doll-with-comb toy-soldier))
  82. ;;;
  83. ;;; + could have a simple-bake-oven set of procedures that takes
  84. ;;; arguments like flavor and dessert-type:
  85. ;;;
  86. ;;; #> (define (simple-bake-oven flavor dessert-type)
  87. ;;; (format #f "Yum! You made a tasty ~a flavored ~a!"
  88. ;;; flavor dessert-type))
  89. ;;; #> (simple-bake-oven "banana" "cake")
  90. ;;; $20 = "Yum! You made a tasty banana flavored cake!"
  91. ;;;
  92. ;;; and then we can increase the advanced features a bit:
  93. ;;;
  94. ;;; #> (define* (fancy-bake-oven flavor dessert-type
  95. ;;; #:optional topping)
  96. ;;; (if topping
  97. ;;; (format #f "Yum! You made a tasty ~a flavored ~a covered in ~a!"
  98. ;;; flavor dessert-type topping)
  99. ;;; (format #f "Yum! You made a tasty ~a flavored ~a!"
  100. ;;; flavor dessert-type)))
  101. ;;; #> (fancy-bake-oven "mint" "ice cream" "chocolate fudge")
  102. ;;; $21 = "Yum! You made a tasty mint flavored ice cream covered in chocolate fudge!"
  103. ;;;
  104. ;;; Yes... the fancy bake oven version is so fancy it can even bake
  105. ;;; ice cream! ;)
  106. ;;;
  107. ;;; + Introduce modules as extensions for our robots.
  108. ;; - Acclimating ourselves with Scheme's syntax
  109. ;; - Understanding some basic types
  110. (chapter
  111. :title [Simple toys]
  112. (include "simple-toys.skb"))
  113. ;; - Simple function definition with (define)
  114. ;; - Advanced function definition with (define*)
  115. ;; - More advanced function definition
  116. ;; - Introduce lambda
  117. ;; - Let
  118. ;; - Introduce lexical scope
  119. ;; - Introduce modules
  120. ;; - Some complex types (hash tables, vhashes?, vectors)
  121. ;; - IO
  122. ;; - Exploring the manual
  123. (chapter
  124. :title [Building blocks]
  125. ;;
  126. )
  127. ;; - Match
  128. ;; - A little web application
  129. (chapter
  130. :title [Putting on a play]
  131. )
  132. ;; - Why all the parentheses?
  133. ;; - Apply
  134. ;; - Eval
  135. ;; - Macros
  136. (chapter
  137. :title [Toys that make toys]
  138. ;; macros!
  139. )
  140. ;; - GOOPS
  141. ;; - Building our own object oriented system
  142. ;; - Functional vs imperative programming
  143. ;; - Where to go from here?
  144. (chapter
  145. :title [The grand finale])
  146. (chapter
  147. :title [About this tutorial]
  148. (p [This tutorial is released under the
  149. ,(ref :url [https://www.gnu.org/licenses/lgpl.html]
  150. :text [GNU LGPL]),
  151. version 3 or later,
  152. as published by the
  153. ,(ref :url [http://www.fsf.org/]
  154. :text [Free Software Foundation]).
  155. You can get a complete copy of its source from
  156. ,(ref :url repo-url
  157. :text [its git repository]).
  158. If you have suggestions or improvements, you are welcome to make them to
  159. the ,(ref :url [https://lists.gnu.org/mailman/listinfo/guile-user/]
  160. :text [guile-user mailing list])
  161. Patches welcome!])))