<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nisei Kimura&#039;s Blog &#187; Computer Science</title>
	<atom:link href="http://science.kinokoru.jp/category/computer-science/feed/" rel="self" type="application/rss+xml" />
	<link>http://science.kinokoru.jp</link>
	<description></description>
	<lastBuildDate>Mon, 14 Jul 2014 10:24:03 +0000</lastBuildDate>
	<language>en</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>[Scheme] Checking The Validity of The Number Passed to The List</title>
		<link>http://science.kinokoru.jp/scheme-checking-validity-number-passed-list/</link>
		<comments>http://science.kinokoru.jp/scheme-checking-validity-number-passed-list/#comments</comments>
		<pubDate>Sun, 01 Jun 2014 07:03:39 +0000</pubDate>
		<dc:creator><![CDATA[Nisei]]></dc:creator>
				<category><![CDATA[SICP]]></category>
		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://science.kinokoru.jp/?p=71</guid>
		<description><![CDATA[<p>In many programming languages, when we pass the number n to the array which the length is equal or less than n, we get an exception. The following is the example of C: int main(void){ int hoge[] = {0,1,2,3}; printf("%d", hoge[3]); // output: 3 printf("%d", hoge[4]); // get a warning return 0; } We can [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/scheme-checking-validity-number-passed-list/">[Scheme] Checking The Validity of The Number Passed to The List</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>In many programming languages,<br />
when we pass the number n to the array which the length is equal or less than n, we get an exception.</p>
<p>The following is the example of C:</p>
<pre>

int main(void){
    int hoge[] = {0,1,2,3};
    printf("%d", hoge[3]); // output: 3
    printf("%d", hoge[4]); // get a warning
    return 0;
}

</pre>
<p>We can implement this kind of exception in Scheme by using the procedure length which is introduced in SICP 2.2.1.</p>
<p>The code will be as follows: </p>
<pre>

; returns the length of a list
(define (length li)
  (define (length-iter n iter-li)
    (if (null? iter-li)
      n
      (length-iter (+ n 1) (cdr iter-li))))
  (length-iter 0 li))

; check if the value passed to the list is valid
(define (cdr-chk-err li n)
  (cond ((or (> n (length li)) (= n (length li)))
    (error "Invalid value passed to the list. "))))

(define (list-ref n)
  (lambda (x)
    (cdr-chk-err x n)
    (if (= n 0)
      (car x)
      ((list-ref (- n 1)) (cdr x)))))

</pre>
<p>This works correctly.</p>
<pre>

(define l1 (list 1 2 3 4))
((list-ref 3) l1) ; returns 4
((list-ref 4) l1) ; error!

</pre>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/scheme-checking-validity-number-passed-list/">[Scheme] Checking The Validity of The Number Passed to The List</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://science.kinokoru.jp/scheme-checking-validity-number-passed-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[SICP 2.1.4] Some Observation about Exercise 2.16</title>
		<link>http://science.kinokoru.jp/sicp-2-1-4-observation-exercise-2-16/</link>
		<comments>http://science.kinokoru.jp/sicp-2-1-4-observation-exercise-2-16/#comments</comments>
		<pubDate>Sat, 31 May 2014 10:32:44 +0000</pubDate>
		<dc:creator><![CDATA[Nisei]]></dc:creator>
				<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://science.kinokoru.jp/?p=69</guid>
		<description><![CDATA[<p>First time I saw this problem, I wondered how it works when I use fractions instead of decimals until the final operation is executed( if the number is not accurate one, then convert it to the fraction like 3.14 to 314 / 100 ). To examine this, let&#8217;s define a function calculating some complex value. [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-2-1-4-observation-exercise-2-16/">[SICP 2.1.4] Some Observation about Exercise 2.16</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>First time I saw this problem, I wondered how it works when I use fractions instead of decimals until the final operation is executed( if the number is not accurate one, then convert it to the fraction like 3.14 to 314 / 100 ).</p>
<p>To examine this, let&#8217;s define a function calculating some complex value.</p>
<pre> 

(define (g n)
  (define (g-iter result i)
    (if (= i n)
      result
      (g-iter (+ (/ (/ (/ 312423 125531) (/ 2052712 7231400)) (/ 13222 113131)) result) (+ i 1)
  )))
  (g-iter 1 1)
)

</pre>
<p>The result of the operation is returned as a fraction:<br />
(19358134129159 * 750189) * 5180721713949/19358134129159</p>
<p>Each term is absolutely determined value.<br />
For getting the decimal number from this fraction value, we just need to multiply this by 1.0.</p>
<pre>

(* 1.0 (g 10000)) ; 750189.2676250552

</pre>
<p>If we use the decimal number in the calculation, the different result will show up:</p>
<pre>

(define (g n)
  (define (g-iter result i)
    (if (= i n)
      result
      (g-iter (+ (/ (/ (/ 312423.0 125531) (/ 2052712 7231400)) (/ 13222 113131)) result) (+ i 1)
  )))
  (g-iter 0 0)
)

(g 10000) ; 750189.2676250958

</pre>
<p>Then which number is more near the actual value?<br />
Let&#8217;s calculate ((((312423 / 125531) / (2052712 / 7231400)) / (13222 / 113131)) * 10000) in the Wolframalpha.<br />
<a href="http://www.wolframalpha.com/input/?i=%28%28%28%28312423+%2F+125531%29+%2F+%282052712+%2F+7231400%29%29+%2F+%2813222+%2F+113131%29%29+*+10000%29" target="blank">Calc result</a></p>
<pre>
750189.2676250551516388667249372079289168657014682584005287499...
</pre>
<p>It seems that the former one is more accurate.</p>
<p>However, the fraction-version calculation will lose its speed when the number of calculation grows up. If we try to execute function g 10000000 times, the response of the fraction-version one is cruel in contrast to the decimal-version one which outputs the result within 0.5s.</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-2-1-4-observation-exercise-2-16/">[SICP 2.1.4] Some Observation about Exercise 2.16</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://science.kinokoru.jp/sicp-2-1-4-observation-exercise-2-16/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[SICP 2.1.4] Exercise 2.11 Memo</title>
		<link>http://science.kinokoru.jp/sicp2-1-4-exercise-2-11-memo/</link>
		<comments>http://science.kinokoru.jp/sicp2-1-4-exercise-2-11-memo/#comments</comments>
		<pubDate>Fri, 30 May 2014 08:39:02 +0000</pubDate>
		<dc:creator><![CDATA[Nisei]]></dc:creator>
				<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://science.kinokoru.jp/?p=62</guid>
		<description><![CDATA[<p>Exercise 2.11: Rewrite procedure mul-interval using Ben&#8217;s suggestion. The default mul-interval executes 4 multiplications on every case of operations, because the process calculates p1 to p4 regardless of the signs of the endpoints. So, the objective of this problem is to minimize the number of operations. (define (mul-interval x y) (let ((mi make-interval) (lx (lower-bound [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp2-1-4-exercise-2-11-memo/">[SICP 2.1.4] Exercise 2.11 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><strong>Exercise 2.11: Rewrite procedure mul-interval using Ben&#8217;s suggestion. </strong></p>
<p>The default mul-interval executes 4 multiplications on every case of operations, because the process calculates p1 to p4 regardless of the signs of the endpoints.<br />
So, the objective of this problem is to minimize the number of operations.</p>
<pre>

(define (mul-interval x y)
  (let
    ((mi make-interval)
     (lx (lower-bound x))
     (ux (upper-bound x))
     (ly (lower-bound y))
     (uy (upper-bound y)))
  (cond
    ((and (neg-int? x) (neg-int? y))
      (mi (* lx ly) (* ux uy)))
    ((and (neg-int? x) (zero-span? y))
      (mi (* lx uy) (* lx ly)))
    ((and (neg-int? x) (pos-int? y))
      (mi (* ux uy) (* lx ly)))
    ((and (zero-span? x) (neg-int? y))
      (mi (* ux uy) (* lx uy)))
    ((and (zero-span? x) (pos-int? y))
      (mi (* lx uy) (* ux uy)))
    ((and (pos-int? x) (neg-int? y))
      (mi (* ux uy) (* lx ly)))
    ((and (pos-int? x) (zero-span? y))
      (mi (* ux ly) (* ux uy)))
    ((and (pos-int? x) (pos-int? y))
      (mi (* lx ly) (* ux uy)))
    ((and (zero-span? x) (zero-span? y))
      (mi (min (* lx uy) (* ux ly)) (max (* lx ly) (* ux uy)))))))

(define i1 (make-interval -5 3))
(define i2 (make-interval -2 110))
(mul-interval i1 i2) ; (-550, 330)

</pre>
<p>Note that <strong>there will be no point to rewrite the procedure if you write the multiplication as the variable defined in the `let`, because variables we define in `let` are all executed whether or not we use them</strong>. In this case, if you write as follows, 4 multiplications are executed in mul-interval every time:</p>
<pre>
(define (mul-interval x y)
  (let
    ((mi make-interval)
     (lxly (* (lower-bound x) (lower-bound y)))
     (lxuy (* (lower-bound x) (upper-bound y)))
     (uxly (* (upper-bound x) (lower-bound y)))
     (uxuy (* (upper-bound x) (upper-bound y))))
  :
  :
  :

</pre>
<p>If you want to confirm this kind of behavior of `let`, try defining (zero-div (/ 1 0)) as an variable of `let`, and you&#8217;ll soon get zero-division error even if you aren&#8217;t using variable zero-div in the process.</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp2-1-4-exercise-2-11-memo/">[SICP 2.1.4] Exercise 2.11 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://science.kinokoru.jp/sicp2-1-4-exercise-2-11-memo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[SICP 2.1.4] Some Observation about Exercise 2.10</title>
		<link>http://science.kinokoru.jp/sicp-2-1-4-observation-about-exercise-2-10/</link>
		<comments>http://science.kinokoru.jp/sicp-2-1-4-observation-about-exercise-2-10/#comments</comments>
		<pubDate>Thu, 29 May 2014 09:34:13 +0000</pubDate>
		<dc:creator><![CDATA[Nisei]]></dc:creator>
				<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://science.kinokoru.jp/?p=54</guid>
		<description><![CDATA[<p>Exercise 2.10- Ben Bitdiddle, an expert systems programmer, looks over Alyssa&#8217;s shoulder and comments that it is not clear what it means to divide by an interval that spans zero. Modify Alyssa&#8217;s code to check for this condition and to signal an error if it occurs. There were many blog posts which talks about Exercise [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-2-1-4-observation-about-exercise-2-10/">[SICP 2.1.4] Some Observation about Exercise 2.10</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><strong>Exercise 2.10- Ben Bitdiddle, an expert systems programmer, looks over Alyssa&#8217;s shoulder and comments that it is not clear what it means to divide by an interval that spans zero. Modify Alyssa&#8217;s code to check for this condition and to signal an error if it occurs. </strong></p>
<p>There were many blog posts which talks about Exercise 2.10, and I&#8217;ve found that there were 2 ways of understanding of the problem on the internet.</p>
<ul>
<li>The problem literally tells us to implement an error that occurs when we pass two arguments that spans zero (like -5 and 3).</li>
<li>The problem tells us to implement errors by zero division.</li>
</ul>
<p>So, which one is correct? After a straightforward survey, I&#8217;ve found that the answer seems to be <strong>the former one</strong>.</p>
<p>Then why people feel confused to understand the meaning of the problem?</p>
<p>At a first glance, not a few people might have thought that &#8220;OK, this is an problem that related to the procedure div-interval, right?&#8221;, then tried to pass arguments for div-interval like this:</p>
<pre>

(define i1 (make-interval 5 5))
(define i2 (make-interval -5 5))
(div-interval i1 i2) ; (-1.0 . 1.0)

</pre>
<p>The output seems to work correct ( in fact it does, because the procedure mul-interval properly extracts the result from p1 ~ p4, using the procedures min &#038; mul ). Now we don&#8217;t know what the actual problem is.</p>
<p>The answer is simple. <strong>What we really had to check was not the procedure div-interval but the following process</strong>: </p>
<pre>

(make-interval (/ 1.0 (upper-bound x)) (/ 1.0 (lower-bound x)))

</pre>
<p>Let&#8217;s check the behavior of this process.</p>
<pre>

(define i (make-interval -5 5))
  (make-interval (/ 1.0 (upper-bound i)) (/ 1.0 (lower-bound i))) ; (0.2, -0.2)

</pre>
<p>The output is (0.2, -0.2), and now we know what we have to do is:</p>
<pre></pre>
<p>(define (reciprocal-interval x)<br />
  (if (< (* (upper-bound x) (lower-bound x)) 0)
    (error "The arguments spans 0")  
    (make-interval (/ 1.0 (upper-bound x)) (/ 1.0 (lower-bound x)))))

[/shell]

I think this is the true understanding of the exercise 2.10.

As long as we use the reciprocal-value for only the procedure div-interval, it works well, so we don't need to implement the error function.
However, say we want to implement another procedure using the reciprocal interval --- the tragedy occurs.
So <strong>we have to test the black-box thoroughly even if one of abstract procedures that using the black-box seems to work fine</strong>. They can sometimes hide the flaw of the implementation and make problem difficult, like this exercise.</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-2-1-4-observation-about-exercise-2-10/">[SICP 2.1.4] Some Observation about Exercise 2.10</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://science.kinokoru.jp/sicp-2-1-4-observation-about-exercise-2-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[SICP 2.1.3] Exercise 2.5 Memo</title>
		<link>http://science.kinokoru.jp/sicp-2-1-3-exercise-2-5-memo/</link>
		<comments>http://science.kinokoru.jp/sicp-2-1-3-exercise-2-5-memo/#comments</comments>
		<pubDate>Mon, 26 May 2014 10:21:34 +0000</pubDate>
		<dc:creator><![CDATA[Nisei]]></dc:creator>
				<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://science.kinokoru.jp/?p=51</guid>
		<description><![CDATA[<p>Exercise 2.5- Show that we can represent pairs of nonnegative integers using only numbers and arithmetic operations if we represent the pair a and b as the integer that is the product 2^a3^b. Give the corresponding definitions of the procedures cons, car, and cdr. So far, we&#8217;ve seen an implementation of these procedures using lambda [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-2-1-3-exercise-2-5-memo/">[SICP 2.1.3] Exercise 2.5 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><strong>Exercise 2.5- Show that we can represent pairs of nonnegative integers using only numbers and arithmetic operations if we represent the pair a and b as the integer that is the product 2^a3^b. Give the corresponding definitions of the procedures cons, car, and cdr.</strong></p>
<p>So far, we&#8217;ve seen an implementation of these procedures using lambda expression. By the way, it seems that we can implement them with only numbers and some arithmetic operations according to the problem, so let&#8217;s create that version of the procedures.</p>
<p>First of all, We need a procedure that tells us the power of 2 and 3 from a value.</p>
<pre>
(define (find-times-of-n n x)
  (define (iter dev-x result)
    (let ((r (remainder dev-x n)))
      (if (= r 0) (iter (/ dev-x n) (+ result 1))
                  result)))
  (iter x 0))
</pre>
<p>What we have to do for getting the power of n( in this case, 2 or 3 would be passed ) from value x is only devide n from x until the remainder of the value / n is non-zero.</p>
<p>Next, we&#8217;ll create procedure cons. This is easy because we only need to create a procedure that takes two arguments ( a and b ), and returns 2^a3^b. The procedure will be as follows:</p>
<pre>

(define (pow x n)
  (define (iter i result)
    (if (= i n) result (iter (+ i 1) (* result x))))
  (iter 0 1))

(define (cons x y) (* (pow 2 x) (pow 3 y)))

</pre>
<p>Finally, we&#8217;ll create procedure car and cdr. Let&#8217;s use the procedure find-times-of-n:</p>
<pre>

(define (car cons) (find-times-of-n 2 cons))
(define (cdr cons) (find-times-of-n 3 cons))

</pre>
<p>If we want to get the output, you can implement the procedure like this: </p>
<pre>

(define (print-cons cons)
  (newline)
  (display "(")
  (display (car cons))
  (display ",")
  (display (cdr cons))
  (display ")"))

(define hoge (cons 61 22))
(print-cons hoge) ; (61, 22)

</pre>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-2-1-3-exercise-2-5-memo/">[SICP 2.1.3] Exercise 2.5 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://science.kinokoru.jp/sicp-2-1-3-exercise-2-5-memo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[SICP 1.3.4] Exercise 1.40-1.46 Memo</title>
		<link>http://science.kinokoru.jp/sicp-1-3-4-exercise-1-40-1-46-memo/</link>
		<comments>http://science.kinokoru.jp/sicp-1-3-4-exercise-1-40-1-46-memo/#comments</comments>
		<pubDate>Sat, 24 May 2014 12:29:34 +0000</pubDate>
		<dc:creator><![CDATA[Nisei]]></dc:creator>
				<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://science.kinokoru.jp/?p=47</guid>
		<description><![CDATA[<p>Excercise 1.40 &#8211; Define a procedure cubic that can be used together with the newtons-method procedure in expressions of the form (newtons-method (cubic a b c) 1) to approximate zeros of the cubic x^3 + ax^2 + bx + c. (define (cube x) (* x x x)) (define (square x) (* x x)) (define (cubic [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-1-3-4-exercise-1-40-1-46-memo/">[SICP 1.3.4] Exercise 1.40-1.46 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><strong>Excercise 1.40 &#8211; Define a procedure cubic that can be used together with the newtons-method procedure in expressions of the form (newtons-method (cubic a b c) 1) to approximate zeros of the cubic x^3 + ax^2 + bx + c.</strong></p>
<pre>
(define (cube x) (* x x x))
(define (square x) (* x x))

(define (cubic a b c)
(lambda (x) (+ (cube x) (* a (square x)) (* b x) c)))

(newtons-method (cubic 3 2 5) 1) ; -2.9041608591349113
</pre>
<p>You can see answer of x^3+2x^2+5x+1 = 0 in the following link.<br />
<a href="http://www.wolframalpha.com/input/?i=x%5E3%2B3x%5E2%2B2x%2B5%3D0&amp;dataset=" target="_blank">http://www.wolframalpha.com/input/?i=x%5E3%2B3x%5E2%2B2x%2B5%3D0&amp;dataset=</a></p>
<p><strong>Excercise 1.41 &#8211; Define a procedure double that takes a procedure of one argument as argument and returns a procedure that applies the original procedure twice. For example, if inc is a procedure that adds 1 to its argument, then (double inc) should be a procedure that adds 2. What value is returned by (((double (double double)) inc) 5)<br />
</strong></p>
<pre>
(define (double f)
(lambda (x) (f (f x))))

((double square) 5) ; 625
</pre>
<p>Notice that the answer is 625, not 125 because we apply square for the square of 5(= 25).</p>
<p><strong>Exercise 1.42. Let f and g be two one-argument functions. The composition f after g is defined to be the function x f(g(x)). Define a procedure compose that implements composition. For example, if inc is a procedure that adds 1 to its argument, ((compose square inc) 6) = 49</strong></p>
<pre>
(define (composition f g)
(lambda (x) (f (g x))))

((compose square inc) 6) ; 49
</pre>
<p><span id="more-47"></span></p>
<p><strong>Excercise 1.43 &#8211; If f is a numerical function and n is a positive integer, then we can form the nth repeated application of f, which is defined to be the function whose value at x is f(f(&#8230;(f(x))&#8230;)). For example, if f is the function x x + 1, then the nth repeated application of f is the function x x + n. If f is the operation of squaring a number, then the nth repeated application of f is the function that raises its argument to the 2nth power. Write a procedure that takes as inputs a procedure that computes f and a positive integer n and returns the procedure that computes the nth repeated application of f. Your procedure should be able to be used as follows:<br />
((repeated square 2) 5) ;625</strong></p>
<p>We pass the default value of iteration and procedure itself in repeated-iter.<br />
The result will be executed at last.</p>
<pre>
(define (repeated f n)
(define (repeated-iter i result)
(if (= i n)
(lambda (x) (result x))
(repeated-iter (+ i 1) (double result))))
(repeated-iter 1 f))

((repeated square 2) 5) ; 625
</pre>
<p><strong>Excercise 1.44 &#8211; Write a procedure smooth that takes as input a procedure that computes f and returns a procedure that computes the smoothed f. It is sometimes valuable to repeatedly smooth a function (that is, smooth the smoothed function, and so on) to obtained the n-fold smoothed function. Show how to generate the n-fold smoothed function of any given function using smooth and repeated from exercise 1.43.</strong></p>
<p>Notice that in exercise 1.43, we passed the number 5 to the square procedure as an argument, but this time we&#8217;re passing a procedure to it. At the first time, we feel confused for the diversity of the expression. however you can get the picture of this by imagining that we&#8217;re passing an &#8220;argument&#8221; for the procedure.</p>
<pre>
(define (smooth f)
(lambda (x) (/ (+ (f (+ x dx)) (f x) (f (- x dx))) 3) ))

(define (n-fold-smooth n f)
((repeated smooth n) f))

((n-fold-smooth 5 cube) 3) ; apply smooth procedure 5 times
</pre>
<p><strong>Excercise 1.45 &#8211; Do some experiments to determine how many average damps are required to compute nth roots as a fixed-point search based upon repeated average damping of y |-&gt; x / y^n-1.</strong></p>
<p>First we create a procedure so that we can do the experiment with some small values.</p>
<pre>
(define (n-times-average-damp n)
(repeated average-damp n))

(define (n-sqrt x)
(fixed-point-of-transform (lambda (y) (/ x (* y y ... )))
(n-times-average-damp ... ))
1.0))
</pre>
<p>You see the factorial of y in the function. This means &#8220;n-1&#8243; in the expression.<br />
That is, You can get x^n when the (n-1)th power is excecuted.</p>
<p>Finally we get following result of the experiment.</p>
<p>(power) (required number of average-damp execution)<br />
y^1 = 1 time<br />
y^2 = 1 time<br />
y^3 = 2 times<br />
:<br />
:<br />
y^8 = 3 times<br />
:<br />
:<br />
y^16 = 4 times</p>
<p>So the number of required execution of average-damp will be log_2(n).</p>
<p><strong>Use this to implement a simple procedure for computing nth roots using fixed-point, average-damp, and the repeated procedure of exercie 1.43. Assume that any arithmetic operations you need are available as primitives.<br />
</strong></p>
<p>Because the number should be an integer, we&#8217;re using a primitive procedure floor.</p>
<pre>
(define (log_2 x) (/ (log x) (log 2)))

(define (n-sqrt x n)
(fixed-point-of-transform
(lambda (y) (/ x ((repeated (lambda (x) (* x y)) (- n 1)) 1)))
(n-times-average-damp (floor (log_2 n)))
1.0))
</pre>
<p><strong>Excercise 1.46 &#8211; Write a procedure iterative-improve that takes two procedures as arguments: a method for telling whether a guess is good enough and a method for improving a guess. Iterative-improve should return as its value a procedure that takes a guess as argument and keeps improving the guess until it is good enough.</strong></p>
<pre>
(define (iterative-improve enough? improve)
(lambda (guess)
(define (iter guess)
(let ((next (improve guess)))
(if (enough? next guess)
next
(iter next))))
(iter guess)))
</pre>
<p><strong>Rewrite the sqrt procedure of section 1.1.7 and the fixed-point procedure of section 1.3.3 in terms of iterative-improve. </strong></p>
<p>The sqrt procedure will be as follows:</p>
<pre>
(define (sqrt x)
(define (close-enough? v1 v2)
(&lt; (abs (- v1 v2)) 0.0000001))
(iterative-improve
close-enough?
(lambda (y) (average y (/ x y)))))

((sqrt 3) 1.0); 1.7320508075688772
</pre>
<p>And the fixed-point procedure will be as follows:</p>
<pre>
(define (fixed-point-improved f)
(define (close-enough? v1 v2)
(&lt; (abs (- v1 v2)) 0.0000001))
(iterative-improve
close-enough?
(lambda (x) (average x (f x)))))
</pre>
<p>If you think that you want to use fixed-point-improved procedure in the sqrt procedure, the code will be like this:</p>
<pre>
(define (sqrt x)
(fixed-point-improved (lambda (y) (/ x y))))

((sqrt 3) 1.0) ; 1.7320508075688772
</pre>
<p>&#8220;1.0&#8243; is the number for first guess. Although you can create this in the procedure, it would be efficient sometimes, like when you need to guess numbers with some logarithm ( in this time, we might pass 2.0 instead of 1.0 because log(1) = 0 ).</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-1-3-4-exercise-1-40-1-46-memo/">[SICP 1.3.4] Exercise 1.40-1.46 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://science.kinokoru.jp/sicp-1-3-4-exercise-1-40-1-46-memo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[SICP 1.3.3] Exercise 1.37-1.39 Memo</title>
		<link>http://science.kinokoru.jp/sicp-1-3-3-exercise-1-37-1-39-memo/</link>
		<comments>http://science.kinokoru.jp/sicp-1-3-3-exercise-1-37-1-39-memo/#comments</comments>
		<pubDate>Sun, 18 May 2014 09:33:27 +0000</pubDate>
		<dc:creator><![CDATA[Nisei]]></dc:creator>
				<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://science.kinokoru.jp/?p=41</guid>
		<description><![CDATA[<p>Exercise 1.37(a)- define k-term finite continued fraction. Linear recursive version: (define (cont-frac n d k) (define (cont-frac-iter i) (if (= i k) (/ (n i) (d i)) (/ (n i) (+ (d i) (cont-frac-iter (+ i 1.0))))) ) (cont-frac-iter 1) ) Then how can we check if the number of k is enough for the [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-1-3-3-exercise-1-37-1-39-memo/">[SICP 1.3.3] Exercise 1.37-1.39 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><strong>Exercise 1.37(a)- define k-term finite continued fraction.</strong></p>
<p>Linear recursive version:</p>
<pre>

(define (cont-frac n d k)
  (define (cont-frac-iter i)
    (if (= i k)
      (/ (n i) (d i))
      (/ (n i) (+ (d i) (cont-frac-iter (+ i 1.0)))))
  )
  (cont-frac-iter 1)
)
</pre>
<p>Then how can we check if the number of k is enough for the calculation in the accuracy of 0.0001? We may be able to check it manually, but creating a checking function seems to be better for me.</p>
<pre>

(define (check-accuracy)
  (define (iter k)
    (if (&lt; (abs (- (calc-ratio k) (/ 1 1.6180339887)) ) 0.0001)
      k
      (iter (+ k 1))
    )
  )
  (iter 1)
)

</pre>
<p>And we get 10 iterations to get the accuracy in 0.0001.</p>
<p>In this case, the function passes calc-ratio method without using arguments. You can build an abstraction for checking arbitrary accuracy of arbitrary function if you prefer.</p>
<pre>

(define (check-accuracy accuracy f target)
  (define (iter k)
    (if (&lt; (abs (- (f k) target) ) accuracy)
      k
      (iter (+ k 1))
    )
  )
  (iter 1)
)

</pre>
<p><span id="more-41"></span></p>
<p><strong>Exercise 1.37(b) Write linear-iterative version of cont-frac function ( if you&#8217;ve created the linear-iterative one in the Exercise 1.37(a), then create linear-recursive version )</strong></p>
<p>The linear-recursive version processes the calculation from 1 to n, but we need to change this process from n to 1 for creating the linear-iterative one, because if we try to start from 1 and make it increment the number, the requirement of the result of (i + 1)th process occurs to determine the result of i-th process. Then the function will be as follows:</p>
<pre>

(define (cont-frac-linear-iterative n d k)
  (define (cont-frac-iter numerator i)
    (cond ((= i 1) numerator)
      ((= i k) (cont-frac-iter (/ (n (- i 1.0)) (+ (/ (n i) (d i)) (d (- i 1.0)))) (- i 1.0)))
      (else (cont-frac-iter (/ (n (- i 1.0)) (+ numerator (d (- i 1.0)))) (- i 1.0)))
    )
  )
  (cont-frac-iter 0 k)
)

</pre>
<p>Let&#8217;s check two versions of cont-frac function.</p>
<pre>

(define (calc-ratio k) (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) k))
(define (calc-ratio2 k) (cont-frac-linear-iterative (lambda (x) 1.0) (lambda (x) 1.0) k))

(calc-ratio 100) ; 0.6180339887498948
(calc-ratio2 100) ; 0.6180339887498948

(calc-ratio 10000000) ; used up the memory
(calc-ratio2 10000000) ; 0.6180339887498948

</pre>
<p><strong>Exercise 1.38 &#8211; define the natual logarithm e.</strong></p>
<p>We finally get the expression of the 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, &#8230; as follows:</p>
<pre>

(define (e)
  (+ 2 (cont-frac-linear-iterative
    (lambda (a) 1.0)
    (lambda (a) (if (&lt; (remainder a 3) 2) 1.0 (* (/ 2.0 3.0) (+ a 1))))
    1000
  ))
)

</pre>
<p>The output is:</p>
<pre>

(e) ; 2.7182818284590455

</pre>
<p><a href="http://science.kinokoru.jp/wp-content/uploads/2014/05/スクリーンショット-2014-05-18-18.29.58.png"><img class="alignnone size-full wp-image-44" src="http://science.kinokoru.jp/wp-content/uploads/2014/05/スクリーンショット-2014-05-18-18.29.58.png" alt="スクリーンショット 2014-05-18 18.29.58" width="619" height="399" /></a></p>
<p>(if (&lt; (remainder a 3) 2) 1.0 [calculation] ) means that the element that (= (remainder a 3) 0) or (= (remainder a 3) 1) will be 1.0.</p>
<p>To get the meaning of [calculation] ( that is, (* (/ 2.0 3.0) (+ a 1)) ), we need some conversion of numbers.</p>
<p>&#8212;</p>
<p>n = 2, 5, 8, 11, 14</p>
<p>m = 1, 2, 3, 4, 5</p>
<p>a(m) = 2, 4, 6, 8, 10 = (* 2 m)</p>
<p>&#8212;</p>
<p>then m = (* (/ 1 3 ) ( n + 1 ))</p>
<p>so a(n) = (* 2 m) = (* 2 (* (/ 1 3 ) ( n + 1 ))) = (* (/ 2 3 ) ( n + 1 ))</p>
<p><strong>Exercise 1.39 - Define a procedure (tan-cf x k) that computes an approximation to the tangent </strong><br />
<strong>function based on Lambert&#8217;s formula.</strong></p>
<p>Note that the operand of the calculation in numerators is minus.</p>
<pre>

(define (tan-cf x k)
  (cont-frac-linear-iterative
    (lambda (a) (if (= a 1) x (- (* x x))))
    (lambda (a) (- (* 2 a) 1))
    k
  )
)

</pre>
<p>The output is:</p>
<pre>

(tan-cf 1.0 1000) ; 1.557407724654902

</pre>
<p><a href="http://science.kinokoru.jp/wp-content/uploads/2014/05/スクリーンショット-2014-05-18-18.28.56.png"><img class="alignnone size-full wp-image-43" src="http://science.kinokoru.jp/wp-content/uploads/2014/05/スクリーンショット-2014-05-18-18.28.56.png" alt="スクリーンショット 2014-05-18 18.28.56" width="622" height="401" /></a></p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-1-3-3-exercise-1-37-1-39-memo/">[SICP 1.3.3] Exercise 1.37-1.39 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://science.kinokoru.jp/sicp-1-3-3-exercise-1-37-1-39-memo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[SICP 1.3.3] Exercise 1.35-1.36 Memo</title>
		<link>http://science.kinokoru.jp/sicp-1-3-3-exercise-1-35-1-36-memo/</link>
		<comments>http://science.kinokoru.jp/sicp-1-3-3-exercise-1-35-1-36-memo/#comments</comments>
		<pubDate>Sat, 17 May 2014 07:21:35 +0000</pubDate>
		<dc:creator><![CDATA[Nisei]]></dc:creator>
				<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://science.kinokoru.jp/?p=37</guid>
		<description><![CDATA[<p>Exercise 1.35 Show that the golden ratio is a fixed point of  the transformation x&#124;-&#62; 1 + 1/x, and use this fact to compute the golden ration by means of the fixed-point procedure. The following code works correctly. (define (golden-ratio) (fixed-point (lambda (y) (avg y (+ 1 (/ 1 y)))) 1.0)) (golden-ratio) ; 1.6180311591702674 Exercise 1.36 [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-1-3-3-exercise-1-35-1-36-memo/">[SICP 1.3.3] Exercise 1.35-1.36 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><strong>Exercise 1.35 Show that the golden ratio is a fixed point of  the transformation x|-&gt; 1 + 1/x, and use this fact to compute the golden ration by means of the fixed-point procedure.</strong></p>
<p>The following code works correctly.</p>
<pre>

(define (golden-ratio) (fixed-point (lambda (y) (avg y (+ 1 (/ 1 y)))) 1.0))

(golden-ratio) ; 1.6180311591702674

</pre>
<p><strong>Exercise 1.36 Modify fixed-point so that it prints the sequence of approximations it generates, using the newline and display primitives shown in exercise 1.22.</strong></p>
<p>To show each output with the newline and  display primitives, we only need to change the fixed-point method as follows:</p>
<pre>

(define (fixed-point f first-guess)
(define (close-enough? v1 v2) (&lt; (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
; add two methods below to the existing code.
(display guess)
(newline)
:
:

</pre>
<p>Let&#8217;s try if the newly implemented feature works:</p>
<pre>

(golden-ratio)

</pre>
<p>And we&#8217;ll get the following output:</p>
<pre>

1.0
1.5
1.5833333333333333
1.6074561403508771
1.6147785476652068
1.61702925556443
1.617723628348796
1.6179380934832117
1.6180043565683029
1.6180248320058461
1.6180311591702674

</pre>
<p><span id="more-37"></span></p>
<p><strong>Exercise 1.36( sequel ) Find a solution to x^x = 1000 by finding a fixed point of x |-&gt; log(1000) / log(x).</strong></p>
<p><strong> </strong>The following code will work. In this case, we need to change the initial number to 2.0 because the base of logarithm is 2.</p>
<pre>

(define (xexp) (fixed-point (lambda (x) (/ (log 1000) (log x))) 2.0))

</pre>
<p>Then we get the following output:</p>
<pre>

2.0
5.9828921423310435
4.922168721308343
4.628224318195455
4.568346513136242
4.5577305909237005
4.555909809045131
4.555599411610624
4.5555465521473675
4.555537551999825

</pre>
<p>Finally we get 4.555537551999825, and we can confirm if we&#8217;ve got a correct one by means of google search.</p>
<p><a href="http://science.kinokoru.jp/wp-content/uploads/2014/05/スクリーンショット-2014-05-17-16.12.44.png"><img class="alignnone size-full wp-image-38" src="http://science.kinokoru.jp/wp-content/uploads/2014/05/スクリーンショット-2014-05-17-16.12.44.png" alt="スクリーンショット 2014-05-17 16.12.44" width="654" height="418" /></a></p>
<p><strong>Exercise 1.36( sequel ) Compare the number of steps this takes with and without average damping.</strong></p>
<p>In the previous solution, we&#8217;ve got 46 outputs, and we still have room for improvement using average damping. Then let&#8217;s change the code( Just adding a little bit of  arithmetics will do ).</p>
<pre>

(define (xexp) (fixed-point (lambda (x) (avg x (/ (log 1000) (log x)))) 2.0))

</pre>
<p>Then the output is:</p>
<pre>

2.0
5.9828921423310435
4.922168721308343
4.628224318195455
4.568346513136242
4.5577305909237005
4.555909809045131
4.555599411610624
4.5555465521473675
4.555537551999825

</pre>
<p>The number of the output is only 10, so it looks like the correction worked.</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-1-3-3-exercise-1-35-1-36-memo/">[SICP 1.3.3] Exercise 1.35-1.36 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://science.kinokoru.jp/sicp-1-3-3-exercise-1-35-1-36-memo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[SICP 1.3.1] Exercise 1.29-1.32 Memo</title>
		<link>http://science.kinokoru.jp/sicp-1-3-1-exercise-1-29-1-32-memo/</link>
		<comments>http://science.kinokoru.jp/sicp-1-3-1-exercise-1-29-1-32-memo/#comments</comments>
		<pubDate>Fri, 16 May 2014 09:06:03 +0000</pubDate>
		<dc:creator><![CDATA[Nisei]]></dc:creator>
				<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://science.kinokoru.jp/?p=31</guid>
		<description><![CDATA[<p>SICP Exercise 1.29 &#8211; Create more accurate method of numerical integration using Simpson&#8217;s Rule. The following process works. (define (sympson f a b n) (define (term-num x) (if (even? x) 2 4) ) (define (y k) ( f (+ a (* k h)))) (define h (/ (- b a) n)) (define (next x) (+ x [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-1-3-1-exercise-1-29-1-32-memo/">[SICP 1.3.1] Exercise 1.29-1.32 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><strong>SICP Exercise 1.29 &#8211; Create more accurate method of numerical integration using Simpson&#8217;s Rule.</strong><br />
The following process works.</p>
<pre>
(define (sympson f a b n)
(define (term-num x)
(if (even? x) 2 4)
)
(define (y k) ( f (+ a (* k h))))
(define h (/ (- b a) n))
(define (next x) (+ x 1))

(define (term x)
(* (term-num x) ( y x ))
)

(* (/ h 3.0) (+ (y 0) (sum term 1 next (- n 1)) (y n)))
)
[shell]
Then let's compare this method to the one which isn't using Simpson's Rule. First we write the code as follows:
[shell]
(integral cube 0 1 0.001)
(sympson cube 0 1 10)
(sympson cube 0 1 100)
</pre>
<p>And we&#8217;ll get the following output.</p>
<pre>
0.24999987500000073
0.25
0.25
</pre>
<p>It looks like the new one outperforms the previous one.</p>
<p><strong>SICP Exercise 1.30 &#8211; Rewrite sum method as a linear iterative process.</strong><br />
You can easily solve this by using what we&#8217;ve learned so far.</p>
<pre>
(define (sum term a next b)
(define (sum-iter a result)
(if (&gt; a b) result
(sum-iter (next a) (+ result (term a))))
)
(sum-iter a 0)
)
</pre>
<p><span id="more-31"></span></p>
<p><strong>SICP Exercise 1.31(a) &#8211; Write an analogous procedure called product that returns the product of the values of a function at points over a given range. Show how to define factorial in terms of product. Also use product to compute approximationas to PI using the formula: PI/4 = (2*4*4*6*6*8*&#8230;) / (3*3*5*5*7*7*&#8230;)</strong></p>
<p>We can get the definition of product by changing sum method we&#8217;ve created just before.</p>
<pre>
(define (product term a next b)
(define (product-iter a result)
(if (&gt; a b) result
(product-iter (next a) (* result (term a))))
)
(product-iter a 1)
)
</pre>
<p>Then the definition of factorial is:</p>
<pre>
(define (factorial n)
(product (lambda (x) x) 1 (lambda (x) (+ x 1)) n)
)
</pre>
<p>And the definition of the quarter of PI is:</p>
<pre>
(define (qrt-pi)
(define (term x)
(if (even? x) (/ (+ x 2.0) (+ x 1)) (/ (+ x 1.0) (+ x 2)))
)
(define (next x) (+ x 1) )
(product term 1 next 30000)
)
</pre>
<p>To confirm these codes, we write as follows:</p>
<pre>
; Factorial
(factorial 7) ; 5040
; PI
(* 4.0 (qrt-pi)) ; 3.14.....
</pre>
<p>It looks like this works well.</p>
<p><strong>SICP Exercise 1.32(a) &#8211; Show that sum and product are both special cases of a still more general notion called accumulate that combines a collection of terms, using some general accumulation function: </strong></p>
<pre>
(accumulate combiner null-value term a next b)
</pre>
<p>So let&#8217;s create this by using linear iterative process. We can easily get the abstraction by watching two functions( sum, product ) and extracting the common points.</p>
<pre>
(define (accumulate combiner null-value term a next b)
(define (iter a result)
(if (&gt; a b) result
(iter (next a) (combiner result (term a))))
)
(iter a null-value)
)
</pre>
<p>Then two functions will be as follows:</p>
<pre>
(define (sum term a next b)
(accumulate + 0 term a next b)
)

(define (product term a next b)
(accumulate * 1 term a next b)
)
</pre>
<p>Now let&#8217;s check their behavior.</p>
<pre>
(define (total term from to)
(sum term from (lambda (x) (+ x 1)) to)
)

(define (factorial n)
(product (lambda (x) x) 1 (lambda (x) (+ x 1)) n)
)

(total (lambda (x) x) 1 10) ; 55
(total (lambda (x) (* x x)) 1 5) ; 55
(factorial 7) ; 5040
</pre>
<p>Voila.</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-1-3-1-exercise-1-29-1-32-memo/">[SICP 1.3.1] Exercise 1.29-1.32 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://science.kinokoru.jp/sicp-1-3-1-exercise-1-29-1-32-memo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[SICP 1.2.4] Exercise 1.17 Memo</title>
		<link>http://science.kinokoru.jp/sicp-1-2-4-exercise-1-17-memo/</link>
		<comments>http://science.kinokoru.jp/sicp-1-2-4-exercise-1-17-memo/#comments</comments>
		<pubDate>Mon, 12 May 2014 14:02:58 +0000</pubDate>
		<dc:creator><![CDATA[Nisei]]></dc:creator>
				<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://science.kinokoru.jp/?p=23</guid>
		<description><![CDATA[<p>Exercise 1.17. Using the results of exercises 1.16 and 1.17, devise a procedure that generates an iterative process for multiplying two integers in terms of adding, doubling, and halving and uses a logarithmic number of steps. In the exercise, the multiplication is implemented as follows: (define (mul-1 a b) (if (= b 0) 0 (+ [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-1-2-4-exercise-1-17-memo/">[SICP 1.2.4] Exercise 1.17 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><strong>Exercise 1.17. Using the results of exercises 1.16 and 1.17, devise a procedure that generates an iterative process for multiplying two integers in terms of adding, doubling, and halving and uses a logarithmic number of steps.</strong></p>
<p>In the exercise, the multiplication is implemented as follows: </p>
<pre>
(define (mul-1 a b)
  (if (= b 0) 0 (+ a (mul-1 a (- b 1))))
)
</pre>
<p>This requires the order of b. The question here is that we want to implement the same function that requires the order proportional to some logarithm.</p>
<pre>
(define (double x) (+ x x)) 
(define (halve x) (/ x 2))
(define (mul-2 a b)
  (cond ((= b 0) 0)
        ((even? b) (mul-2 (double a) (halve b)))
        (else (+ a (mul-2 a (- b 1)))))
)
</pre>
<p>This only requires the order of log_2(b).</p>
<p>As a result, we get the following output: </p>
<pre>
(mul-1 100 5000000) ; memory shortage
(mul-2 100 5000000) ; 500000000
(mul-2 10000 50000000000000) ; 500000000000000000
</pre>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-1-2-4-exercise-1-17-memo/">[SICP 1.2.4] Exercise 1.17 Memo</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://science.kinokoru.jp/sicp-1-2-4-exercise-1-17-memo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
