-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsicp-exercise-111.html
242 lines (212 loc) · 10.5 KB
/
sicp-exercise-111.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>SICP Exercise 1.11</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="Wonseok Choi">
<!-- Le styles -->
<link rel="stylesheet" href="/theme/css/bootstrap.min.css" type="text/css" />
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
.sidebar-nav {
padding: 9px 0;
}
.tag-1 {
font-size: 13pt;
}
.tag-2 {
font-size: 10pt;
}
.tag-2 {
font-size: 8pt;
}
.tag-4 {
font-size: 6pt;
}
</style>
<link href="/theme/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="/theme/css/font-awesome.css" rel="stylesheet">
<link href="/theme/css/pygments.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="/theme/images/favicon.ico">
<link rel="apple-touch-icon" href="/theme/images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="/theme/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="/theme/images/apple-touch-icon-114x114.png">
<link href="/" type="application/atom+xml" rel="alternate" title="Svperbeast Blog ATOM Feed" />
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="/index.html">Svperbeast Blog </a>
<div class="nav-collapse">
<ul class="nav">
<li class="divider-vertical"></li>
<li >
<a href="/category/blog.html">
<i class="icon-folder-open icon-large"></i>Blog
</a>
</li>
<li class="active">
<a href="/category/sicp.html">
<i class="icon-folder-open icon-large"></i>SICP
</a>
</li>
<ul class="nav pull-right">
<li><a href="/archives.html"><i class="icon-th-list"></i>Archives</a></li>
</ul>
</ul>
<!--<p class="navbar-text pull-right">Logged in as <a href="#">username</a></p>-->
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="span9" id="content">
<section id="content">
<article>
<header>
<h1>
<a href=""
rel="bookmark"
title="Permalink to SICP Exercise 1.11">
SICP Exercise 1.11
</a>
</h1>
</header>
<div class="entry-content">
<div class="well">
<footer class="post-info">
<span class="label">Date</span>
<abbr class="published" title="2013-02-24T22:18:00+09:00">
<i class="icon-calendar"></i>일 24 2월 2013
</abbr>
<span class="label">By</span>
<a href="/author/wonseok-choi.html"><i class="icon-user"></i>Wonseok Choi</a>
<span class="label">Category</span>
<a href="/category/sicp.html"><i class="icon-folder-open"></i>SICP</a>.
<span class="label">Tags</span>
<a href="/tag/sicp.html"><i class="icon-tag"></i>sicp</a>
</footer><!-- /.post-info --> </div>
<div class="highlight"><pre><span></span>f(n) = n if n > 3
f(n) = f(n-1) + 2f(n-2) + 3f(n-3) if n >= 3
</pre></div>
<p>f(n)을 recursive process와 iterative process로 작성하는 문제이다.</p>
<p>내가 기억하기로는 (책에서), 조엘 스폴스키는 C와 같은 언어를 이용해서
recursive function을 만드는 사람을 채용하지 않는다고 했는데,
stack overflow가 발생할 수 있기 때문이다.
일정한 space만 소비하는 iterative process를 구현해야만 하는 이유이다.</p>
<p>SICP Exercise 1.11</p>
<div class="highlight"><pre><span></span><span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">fr</span> <span class="nv">n</span><span class="p">)</span>
<span class="p">(</span><span class="k">cond </span><span class="p">((</span><span class="nb">< </span><span class="nv">n</span> <span class="mi">3</span><span class="p">)</span> <span class="nv">n</span><span class="p">)</span>
<span class="p">(</span><span class="k">else </span><span class="p">(</span><span class="nb">+ </span><span class="p">(</span><span class="nf">fr</span> <span class="p">(</span><span class="nb">- </span><span class="nv">n</span> <span class="mi">1</span><span class="p">))</span>
<span class="p">(</span><span class="nb">* </span><span class="mi">2</span> <span class="p">(</span><span class="nf">fr</span> <span class="p">(</span><span class="nb">- </span><span class="nv">n</span> <span class="mi">2</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">* </span><span class="mi">3</span> <span class="p">(</span><span class="nf">fr</span> <span class="p">(</span><span class="nb">- </span><span class="nv">n</span> <span class="mi">3</span><span class="p">)))))))</span>
<span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">fi</span> <span class="nv">n</span><span class="p">)</span>
<span class="p">(</span><span class="nf">fi-iter</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">2</span> <span class="nv">n</span><span class="p">))</span>
<span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">fi-iter</span> <span class="nv">a</span> <span class="nv">b</span> <span class="nv">c</span> <span class="nv">counter</span><span class="p">)</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">= </span><span class="nv">counter</span> <span class="mi">0</span><span class="p">)</span>
<span class="nv">a</span>
<span class="p">(</span><span class="nf">fi-iter</span> <span class="nv">b</span>
<span class="nv">c</span>
<span class="p">(</span><span class="nb">+ </span><span class="p">(</span><span class="nb">* </span><span class="mi">3</span> <span class="nv">a</span><span class="p">)</span>
<span class="p">(</span><span class="nb">* </span><span class="mi">2</span> <span class="nv">b</span><span class="p">)</span>
<span class="nv">c</span><span class="p">)</span>
<span class="p">(</span><span class="nb">- </span><span class="nv">counter</span> <span class="mi">1</span><span class="p">))))</span>
</pre></div>
</div><!-- /.entry-content -->
<div class="comments">
<h2>Comments !</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_identifier = "sicp-exercise-111.html";
(function() {
var dsq = document.createElement('script');
dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'https://svperbeast-blog.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] ||
document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
</div>
</article>
</section>
</div><!--/span-->
<div class="span3 well sidebar-nav" id="sidebar">
<ul class="nav nav-list">
<li class="nav-header"><h4><i class="icon-home icon-large"></i> social</h4></li>
<li><a href="http://github.com/svperbeast"><i class="icon-github-sign icon-large"></i>github</a></li>
<li class="nav-header"><h4><i class="icon-folder-close icon-large"></i>Categories</h4></li>
<li>
<a href="/category/blog.html">
<i class="icon-folder-open icon-large"></i>Blog
</a>
</li>
<li>
<a href="/category/sicp.html">
<i class="icon-folder-open icon-large"></i>SICP
</a>
</li>
<li class="nav-header"><h4><i class="icon-tags icon-large"></i>Tags</h4></li>
<li class="tag-4">
<a href="/tag/library_version.html">
<i class="icon-tag icon-large"></i>library_version
</a>
</li>
<li class="tag-1">
<a href="/tag/sicp.html">
<i class="icon-tag icon-large"></i>sicp
</a>
</li>
<li class="tag-4">
<a href="/tag/struct.html">
<i class="icon-tag icon-large"></i>struct
</a>
</li>
<li class="tag-4">
<a href="/tag/cmake.html">
<i class="icon-tag icon-large"></i>cmake
</a>
</li>
</ul> </div><!--/.well -->
</div><!--/row-->
<hr>
<footer>
<address id="about">
Proudly powered by <a href="http://pelican.notmyidea.org/">Pelican <i class="icon-external-link"></i></a>,
which takes great advantage of <a href="http://python.org">Python <i class="icon-external-link"></i></a>.
</address><!-- /#about -->
<p>The theme is from <a href="http://twitter.github.com/bootstrap/">Bootstrap from Twitter <i class="icon-external-link"></i></a>,
and <a href="http://fortawesome.github.com/Font-Awesome/">Font-Awesome <i class="icon-external-link"></i></a>, thanks!</p>
</footer>
</div><!--/.fluid-container-->
<script type="text/javascript">
var disqus_shortname = 'svperbeast-blog';
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<!-- Le javascript -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="/theme/js/jquery-1.7.2.min.js"></script>
<script src="/theme/js/bootstrap.min.js"></script>
</body>
</html>