citizen428.blog()

Try to learn something about everything

SICP 1.12, Kind Of...

When working through SICP while you are tired, you might end up solving the wrong problems. This happened to me today with exercise 1.12, which asks the reader to “[w]rite a procedure that computes elements of Pascal’s triangle by means of a recursive process”. I somehow missed the “computes elements” part, and instead wrote a program which generates Pascal’s triangle up to a certain row. Anyway, here it is as a reference for the next guy who can’t read:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<span class='line'><span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">pascal-sums</span> <span class="nv">lst</span><span class="p">)</span>
</span><span class='line'>  <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nf">empty?</span> <span class="p">(</span><span class="nb">cdr </span><span class="nv">lst</span><span class="p">))</span>
</span><span class='line'>      <span class="o">&#39;</span><span class="p">()</span>
</span><span class='line'>      <span class="p">(</span><span class="nb">cons </span><span class="p">(</span><span class="nb">+ </span><span class="p">(</span><span class="nb">car </span><span class="nv">lst</span><span class="p">)</span> <span class="p">(</span><span class="nb">cadr </span><span class="nv">lst</span><span class="p">))</span>
</span><span class='line'>            <span class="p">(</span><span class="nf">pascal-sums</span> <span class="p">(</span><span class="nb">cdr </span><span class="nv">lst</span><span class="p">)))))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">pascal-triangle</span> <span class="nv">depth</span><span class="p">)</span>
</span><span class='line'>  <span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">triangle-iter</span> <span class="nv">lst</span> <span class="nv">n</span> <span class="nv">acc</span><span class="p">)</span>
</span><span class='line'>    <span class="p">(</span><span class="k">cond </span><span class="p">((</span><span class="nb">zero? </span><span class="nv">n</span><span class="p">)</span>
</span><span class='line'>           <span class="p">(</span><span class="nb">reverse </span><span class="nv">acc</span><span class="p">))</span>
</span><span class='line'>          <span class="p">(</span><span class="nf">else</span>
</span><span class='line'>           <span class="p">(</span><span class="nf">triangle-iter</span> <span class="o">`</span><span class="p">(</span><span class="mi">1</span> <span class="o">,@</span><span class="p">(</span><span class="nf">pascal-sums</span> <span class="nv">lst</span><span class="p">)</span> <span class="mi">1</span><span class="p">)</span>
</span><span class='line'>                          <span class="p">(</span><span class="nf">sub1</span> <span class="nv">n</span><span class="p">)</span>
</span><span class='line'>                          <span class="p">(</span><span class="nb">cons </span><span class="nv">lst</span> <span class="nv">acc</span><span class="p">)))))</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">triangle-iter</span> <span class="o">&#39;</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="nv">depth</span> <span class="o">&#39;</span><span class="p">()))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; &gt; (time (pascal-triangle 7))</span>
</span><span class='line'><span class="c1">;; cpu time: 0 real time: 0 gc time: 0</span>
</span><span class='line'><span class="c1">;; ((1) (1 1) (1 2 1) (1 3 3 1) (1 4 6 4 1) (1 5 10 10 5 1) (1 6 15 20 15 6 1))</span>
</span>

Comments

Copyright © 2016 - Michael Kohl - Powered by Octopress