/static/course_material/67/71/content.html

https://github.com/darrenkuo/SICP · HTML · 127 lines · 110 code · 17 blank · 0 comment · 0 complexity · be5a5cb345375e7dec91414f41a894a7 MD5 · raw file

  1. <b>CS 61A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Week 6</b>
  2. <div class="p"><!----></div>
  3. Topic: Generic Operators
  4. <div class="p"><!----></div>
  5. <b>Reading:</b>
  6. Abelson &amp; Sussman, Sections 2.4 through 2.5.2 (pages 169-200)
  7. <div class="p"><!----></div>
  8. <b>Homework:</b>
  9. <div class="p"><!----></div>
  10. Abelson &amp; Sussman, exercises 2.74, 2.75, 2.76, 2.77, 2.79, 2.80, 2.81, 2.83
  11. <div class="p"><!----></div>
  12. Note: Some of these are thought-exercises; you needn't actually run any Scheme
  13. programs for them! (Some don't ask you to write procedures at all;
  14. others ask for modifications to a program that isn't online.)
  15. <div class="p"><!----></div>
  16. <font face="symbol"></font
  17. > If you haven't already finished this week's lab exercises that
  18. involve the <tt>scheme-1</tt> interpreter, do it now. Then write
  19. a <tt>map</tt> primitive for <tt>scheme-1</tt> (call it <tt>map-1</tt> so you
  20. and Scheme don't get confused about which is which) that works
  21. correctly for all mapped procedures.
  22. <div class="p"><!----></div>
  23. <font face="symbol"></font
  24. > Modify the <tt>scheme-1</tt> interpreter
  25. to add the <tt>let</tt> special form. Hint: Like a procedure call, <tt>let</tt>
  26. will have to use <tt>substitute</tt> to replace certain variables with their
  27. values. Don't forget to evaluate the expressions that provide those
  28. values!
  29. <div class="p"><!----></div>
  30. <b>Extra for experts:</b>
  31. <div class="p"><!----></div>
  32. Another approach to the problem of type-handling is <i>type inference</i>.
  33. If, for instance, a procedure includes the expression <tt>(+ n k)</tt>,
  34. one can infer that <tt>n</tt> and <tt>k</tt> have numeric values.
  35. Similarly, the expression <tt>(f a b)</tt> indicates that the value of <tt>f</tt>
  36. is a procedure.
  37. <div class="p"><!----></div>
  38. Write a procedure called <tt>inferred-types</tt> that, given a definition of a
  39. Scheme procedure as argument, returns a list of information about the
  40. parameters of the procedure. The information list should contain one
  41. element per parameter; each element should be a two-element list whose first
  42. element is the parameter name and whose second element is a word indicating
  43. the type inferred for the parameter. Possible types are listed on the next
  44. page.
  45. <div class="p"><!----></div>
  46. <b>Continued on next page.</b>
  47. <div class="p"><!----></div>
  48. <div class="p"><!----></div>
  49. <b>Week 6 continued...</b>
  50. <div class="p"><!----></div>
  51. <br />
  52. <dl compact="compact">
  53. <dt>
  54. <tt>?</tt></dt>
  55. <dd>
  56. (the type can't be inferred)
  57. </dd>
  58. <dt>
  59. <tt>procedure</tt></dt>
  60. <dd> (the parameter appeared as the first word in an
  61. unquoted expression or as the first argument of <tt>map</tt> or <tt>every</tt>)
  62. </dd>
  63. <dt>
  64. <tt>number</tt></dt>
  65. <dd> (the parameter appeared as an argument of <tt>+</tt>, <tt>-</tt>,
  66. <tt>max</tt>, or <tt>min</tt>)
  67. </dd>
  68. <dt>
  69. <tt>list</tt></dt>
  70. <dd> (the parameter appeared as an argument of <tt>append</tt>
  71. or as the second argument of <tt>map</tt> or <tt>member</tt>)
  72. </dd>
  73. <dt>
  74. <tt>sentence-or-word</tt></dt>
  75. <dd> (the parameter appeared as an argument of <tt>first,</tt>
  76. <tt>butfirst,</tt> <tt>sentence,</tt> or <tt>member?</tt>, or
  77. as the second argument of <tt>every</tt>)
  78. </dd>
  79. <dt>
  80. <tt>x</tt></dt>
  81. <dd> (conflicting types were inferred)
  82. </dd></dl><br />You should assume for this problem that the body of the procedure to
  83. be examined does not contain any occurrences of <tt>if</tt> or <tt>cond</tt>,
  84. although it may contain arbitrarily nested and quoted expressions.
  85. (A more ambitious inference procedure both would examine a more comprehensive
  86. set of procedures and could infer conditions like "nonempty list".)
  87. <div class="p"><!----></div>
  88. Here's an example of what your inference procedure should return.
  89. <div class="p"><!----></div>
  90. <tt> <pre>(inferred-types
  91. '(define (foo a b c d e f)
  92. (f (append (a b) c '(b c)) (+ 5 d) (sentence (first e) f)) ) )
  93. </pre></tt>
  94. <div class="p"><!----></div>
  95. should return
  96. <div class="p"><!----></div>
  97. <tt> <pre>((a procedure) (b ?) (c list) (d number)
  98. (e sentence-or-word) (f x))
  99. </pre></tt>
  100. <div class="p"><!----></div>
  101. If you're <i>really </i> ambitious, you could maintain a database of
  102. inferred argument types and use it when a procedure you've seen is
  103. invoked by another procedure you're examining!
  104. <div class="p"><!----></div>
  105. <br /><hr />
  106. Unix feature of the week: <tt>du</tt>, <tt>df</tt>, <tt>quota</tt>
  107. <div class="p"><!----></div>
  108. Emacs feature of the week: <tt>M-q</tt> (format paragraphs), <tt>C-M-q</tt> (format Scheme code)
  109. <div class="p"><!----></div>
  110. <div class="p"><!----></div>