PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/scheme/man/Yet Another Scheme Tutorial/www.shido.info/lisp/scheme3_e.html

http://zhangpengpeng.googlecode.com/
HTML | 286 lines | 239 code | 47 blank | 0 comment | 0 complexity | fc390ca5664de3f278d8d94d6fde8d4c MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <meta name="Author" content="Takafumi Shido">
  6. <meta name="keywords" content="Scheme, lists">
  7. <meta name="description" content="List operations on Scheme">
  8. <meta http-equiv="CONTENT-SCRIPT-TYPE" content="text/css;">
  9. <meta name="robots" content="all">
  10. <link rel="stylesheet" href="../shido_e.css" text="text/css">
  11. <link rel="icon" href="http://www.shido.info/images/shido.png" type="image/png">
  12. <title> 3. Making Lists </title>
  13. </head>
  14. <body>
  15. <a name="top"></a>
  16. <p class="header">
  17. <table class='guide'><tr>
  18. <td><a rel=home href='http://www.shido.info/index_e.php'>
  19. <img src='../images/shido_small.png' class='arrow' border=0>HOME</a></td>
  20. <td><a rel=prev href="scheme2_e.html"><img src='../images/left_arrow.gif' class='arrow' border=0>
  21. 2. Using Scheme as a Calculator</a></td>
  22. <td><a rel=up href="idx_scm_e.html"><img src='../images/up_arrow.gif' class='arrow' border=0>Yet Another Scheme Tutorial</a></td>
  23. <td><a rel=next href="scheme4_e.html"><img src='../images/right_arrow.gif' class='arrow' border=0>4. Defining Functions</a></td>
  24. <td><a href='http://www.shido.info/gb/write_guestbook_e.php?ref=lisp/scheme3_e.html&amp;t=%28Scheme%29+Making+Lists' target='new'>
  25. <img src='../images/pencil.gif' class='arrow' border=0>Post Messages</a></td>
  26. </tr></table></p>
  27. <h1> 3. Making Lists </h1>
  28. <hr>
  29. <h2>1. Introduction </h2>
  30. As Scheme belongs to Lisp linguistic family, it is good at list operations.
  31. You should understand lists and list operations thoroughly to master Scheme.
  32. Lists play important roles in recursive functions and higher order functions,
  33. which I will explain in later chapters.
  34. <p>
  35. In this chapter, I will explain basic list operators, such as
  36. <span class='ttb'>cons</span>, <span class='ttb'>car</span>,
  37. <span class='ttb'>cdr</span>, <span class='ttb'>list</span>
  38. and <span class='ttb'>quote</span>.
  39. <h2>2. Cons Cells and Lists</h2>
  40. <a name='cons'>
  41. <h3>2.1. Cons Cells</h3>
  42. First, let me explain about <b>cons cells</b> which are elements of lists.
  43. Cons cells are a memory spaces which storage two addresses.
  44. Cons cells can be made by function <span class='ttb'>cons</span>.<p>
  45. Give <tt>(cons 1 2)</tt> to the front end.
  46. <pre class='samp'>
  47. (cons 1 2)
  48. <span class='response'>;Value 11: (1 . 2)</span>
  49. </pre>
  50. It responds <tt>(1 . 2)</tt>.
  51. Function <span class='ttb'>cons</span> allocates a memory space for two addresses as shown in Figure 1.
  52. and stores the address to 1 in one part and to 2 in the other part.
  53. The part storing the address to 1 is called <span class='ttb'>car</span> part and
  54. that storing the address to 2 is called <span class='ttb'>cdr</span> part.
  55. <tt>Car</tt> and <tt>cdr</tt> are abbreviations of <u>Contents of the Address part of the Register</u>
  56. and <u>Contents of the Decrement part of the Register</u>. These are originated from the names of
  57. memory spaces of the hardware on which Lisp was first implemented.
  58. These names also indicate that the reality of the cons cell is a memory space.
  59. The name cons is an abbreviation of a English term 'construction' for your information.
  60. <p>
  61. <center><img src='cons2.png'><br>Fig 1: A cons cell.</center>
  62. <p>
  63. Cons cells can be beaded.
  64. <pre class='samp'>
  65. (cons 3 (cons 1 2))
  66. <span class='response'>;Value 15: (3 1 . 2)</span>
  67. </pre>
  68. (3 1 . 2) is a convenient notation for (3 . (1 . 2)).
  69. The memory space of this situation is shown in Figure 2.<p>
  70. <center><img src='conss2.png'><br>Figure 2: Beaded cons cells.</center>
  71. <p>
  72. Cons cells can store different kinds of data and can be nested.
  73. <pre class='samp'>
  74. (cons #\a (cons 3 "hello"))
  75. <span class='response'>;Value 17: (#\a 3 . "hello")</span>
  76. (cons (cons 0 1) (cons 1 2))
  77. <span class='response'>;Value 23: ((0 . 1) 2 . 3)</span>
  78. </pre>
  79. This is due to that Scheme manipulates all the data by their addresses.
  80. (<span class='ttb'>#\</span><var>c</var> represents a character <var>c</var>.
  81. For example, <tt>#\a</tt> represents a character <tt>a</tt>.)
  82. <h3>2.2. Lists</h3>
  83. Lists are (beaded) cons cells with the cdr part of the last cons cell being
  84. <strong>'()</strong>.
  85. <strong>'()</strong> is called the empty list, which is included to lists.
  86. Even if the data consists of only one cons cell, it is a list if the cdr part is <tt>'()</tt>.
  87. Figure 3 shows the memory structure of a list <tt><u>(1 2 3)</u></tt>.
  88. <p>
  89. <center><img src='list2.png'><br>Figure 3: Memory structure for a list <u><tt>(1 2 3)</tt></u>.</center>
  90. <p>
  91. Actuary, list can be defined recursively like as follows:
  92. <ol>
  93. <li> <tt>'()</tt> is a list.
  94. <li> If <var>ls</var> is a list and <var>obj</var> is a kind of data,
  95. <span class='ttb'>(cons <var>obj</var> <var>ls</var>)</span> is a list.
  96. </ol>
  97. As lists are data structure defined recursively,
  98. it is reasonable to be used in recursive functions.
  99. <h3>2.3. atoms</h3>
  100. Data structures which do not use cons cells are called <b>atom</b>.
  101. Numbers, characters, strings, vectors, and <tt>'()</tt> are atom.
  102. <tt>'()</tt> is an atom and a list as well.
  103. <h3> Exercise 1</h3>
  104. Make data structures using <tt>cons</tt> that the front end responds like as follows.
  105. <ol>
  106. <li> <tt>("hi" . "everybody")</tt>
  107. <li> <tt>(0)</tt>
  108. <li> <tt>(1 10 . 100)</tt>
  109. <li> <tt>(1 10 100)</tt>
  110. <li> <tt>(#\I "saw" 3 "girls")</tt>
  111. <li> <tt> ("Sum of" (1 2 3 4) "is" 10)</tt>
  112. </ol>
  113. <h2> 3. quote</h2>
  114. All tokens are ready to be evaluated
  115. due to the Scheme's rule of the evaluation that tokens in parentheses are evaluated from inner to outer
  116. and that the value comes out from the outermost parentheses is the value of the S-expression.
  117. A special form named <span class='ttb'>quote</span> is used to protect tokens from evaluation.
  118. It is for giving symbols or lists to a program, which became something else by evaluation.<p>
  119. For instance, while <tt>(+ 2 3)</tt> is evaluated to be 5, <tt>(quote (+ 2 3))</tt> gives a list
  120. <tt><u>(+ 2 3)</u></tt> itself to the program. As <tt>quote</tt> is frequently used,
  121. it is abbreviated as <u><tt>'</tt></u>.<br>
  122. For example:
  123. <ul>
  124. <li><tt>'(+ 2 3)</tt> represents a list <tt><u>(+ 2 3)</u></tt> itself.
  125. <li> <tt>'+</tt> represents a symbol <u><tt>+</tt></u> itself.
  126. </ul>
  127. Actually, <tt>'()</tt> is a quoted empty list, which means that
  128. you should write <tt>'()</tt> to represent an empty list while the interpreter
  129. responds <tt>()</tt> for an empty list.
  130. <h3>3.1. Special forms</h3>
  131. Scheme has two kinds of operators:
  132. One is functions. Functions evaluate all the arguments to return value.
  133. The other is special forms. Special forms are not evaluate all the arguments.
  134. Besides <tt>quote</tt>,
  135. <span class='ttb'>lambda</span>, <span class='ttb'>define</span>,
  136. <span class='ttb'>if</span>, <span class='ttb'>set!</span>, etc. are special forms.
  137. <h2>4. Functions car and cdr</h2>
  138. Functions that returns the car part and the cdr part of a cons cell is called
  139. <span class='ttb'>car</span> and
  140. <span class='ttb'>cdr</span>, respectively.
  141. If the value of <tt>cdr</tt> is a beaded cons cells, the interpreter
  142. prints whole values of the car parts.
  143. If the cdr part of the last cons cell is not <tt>'()</tt>, the value is also shown
  144. after <b>.</b>.
  145. <pre class='samp'>
  146. (car '(1 2 3 4))
  147. <span class='response'>;Value: 1</span>
  148. (cdr '(1 2 3 4))
  149. <span class='response'>;Value 18: (2 3 4)</span>
  150. </pre>
  151. <h3> Exercise 2</h3>
  152. Evaluated following S-expressions.
  153. <ol>
  154. <li> <tt>(car '(0))</tt>
  155. <li> <tt>(cdr '(0))</tt>
  156. <li> <tt> (car '((1 2 3) (4 5 6)))</tt>
  157. <li> <tt> (cdr '(1 2 3 . 4))</tt>
  158. <li> <tt>(cdr (cons 3 (cons 2 (cons 1 '()))))</tt>
  159. </ol>
  160. <h2>5. Function list </h2>
  161. Function <span class='ttb'>list</span> is available to make a list consisting of several elements.
  162. Function <tt>list</tt> takes arbitrary numbers of arguments and returns a list of them.
  163. <pre class='samp'>
  164. (list)
  165. <span class='response'>;Value: ()</span>
  166. (list 1)
  167. <span class='response'>;Value 24: (1)</span>
  168. (list '(1 2) '(3 4))
  169. <span class='response'>;Value 25: ((1 2) (3 4))</span>
  170. (list 0)
  171. <span class='response'>;Value 26: (0)</span>
  172. (list 1 2)
  173. <span class='response'>;Value 27: (1 2)</span>
  174. </pre>
  175. <h2>6. Summary</h2>
  176. This chapter explained about lists and basic operations for lists.
  177. Chapters 1 &ndash; 3 may be boring, I am afraid.
  178. The next chapter is interesting, I hope.
  179. It deals with making functions.
  180. I will explain
  181. <ul>
  182. <li>how to edit source codes using a editor,
  183. <li>how to load the source code to the interpreter, and
  184. <li> how to define functions.
  185. </ul>
  186. <h3>The Answers for Exercises</h3>
  187. <h4>Answer 1</h4>
  188. <pre class='samp'>
  189. ;1
  190. (cons "hi" "everybody")
  191. <span class='response'>;Value 32: ("hi" . "everybody")</span>
  192. ;2
  193. (cons 0 '())
  194. <span class='response'>;Value 33: (0)</span>
  195. ;3
  196. (cons 1 (cons 10 100))
  197. <span class='response'>;Value 34: (1 10 . 100)</span>
  198. ;4
  199. (cons 1 (cons 10 (cons 100 '())))
  200. <span class='response'>;Value 35: (1 10 100)</span>
  201. ;5
  202. (cons #\I (cons "saw" (cons 3 (cons "girls" '()))))
  203. <span class='response'>;Value 36: (#\I "saw" 3 "girls")</span>
  204. ;6
  205. (cons "Sum of" (cons (cons 1 (cons 2 (cons 3 (cons 4 '())))) (cons "is" (cons 10 '()))))
  206. <span class='response'>;Value 37: ("Sum of" (1 2 3 4) "is" 10)</span>
  207. </pre>
  208. <h4>Answer 2</h4>
  209. <pre class='samp'>
  210. ;1
  211. (car '(0))
  212. <span class='response'>;Value: 0</span>
  213. ;2
  214. (cdr '(0))
  215. <span class='response'>;Value: ()</span>
  216. ;3
  217. (car '((1 2 3) (4 5 6)))
  218. <span class='response'>;Value 28: (1 2 3)</span>
  219. ;4
  220. (cdr '(1 2 3 . 4))
  221. <span class='response'>;Value 29: (2 3 . 4)</span>
  222. ;5
  223. (cdr (cons 3 (cons 2 (cons 1 '()))))
  224. <span class='response'>;Value 31: (2 1)</span>
  225. </pre>
  226. <hr>
  227. <p class="footer">
  228. <table class='guide'><tr>
  229. <td><a rel=home href='http://www.shido.info/index_e.php'>
  230. <img src='../images/shido_small.png' class='arrow' border=0>HOME</a></td>
  231. <td><a rel=prev href="scheme2_e.html"><img src='../images/left_arrow.gif' class='arrow' border=0>
  232. 2. Using Scheme as a Calculator</a></td>
  233. <td><a rel=up href="idx_scm_e.html"><img src='../images/up_arrow.gif' class='arrow' border=0>Yet Another Scheme Tutorial</a></td>
  234. <td><a rel=next href="scheme4_e.html"><img src='../images/right_arrow.gif' class='arrow' border=0>4. Defining Functions</a></td>
  235. <td><a href='http://www.shido.info/gb/write_guestbook_e.php?ref=lisp/scheme3_e.html&amp;t=%28Scheme%29+Making+Lists' target='new'>
  236. <img src='../images/pencil.gif' class='arrow' border=0>Post Messages</a></td>
  237. </tr></table></p>
  238. </body>
  239. </html>