114 lines
7.3 KiB
Markdown
114 lines
7.3 KiB
Markdown
---
|
|
title: 練習問題を解いている
|
|
author: kazu634
|
|
date: 2008-09-07
|
|
url: /2008/09/07/_1099/
|
|
wordtwit_post_info:
|
|
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4263;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
|
categories:
|
|
- gauche
|
|
- Lisp
|
|
|
|
---
|
|
<div class="section">
|
|
<p>
|
|
だいぶ再帰に慣れてきた…ように思う。
|
|
</p>
|
|
|
|
<h4>
|
|
for-each-numbers
|
|
</h4>
|
|
|
|
<blockquote>
|
|
<p>
|
|
前章で、リストの中から条件を満たす要素だけを抜き出したリストを返す手続きfilterを書いてみるという問題を出しました。それを使って、「数値とそれ以外のものが混ざっているリストを受け取り、その中の数値だけについて手続きを適用するfor-each-numbersを書いてみてください。
|
|
</p>
|
|
</blockquote>
|
|
|
|
<pre class="syntax-highlight">
|
|
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>my_filter pred lis<span class="synSpecial">)</span>
|
|
<span class="synSpecial">(</span><span class="synStatement">cond</span> [<span class="synSpecial">(</span><span class="synStatement">null</span>? lis<span class="synSpecial">)</span> <span class="synSpecial">'()</span>]
|
|
[<span class="synSpecial">(</span>pred <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">cons</span> <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span>filter pred <span class="synSpecial">(</span><span class="synStatement">cdr</span> lis<span class="synSpecial">)))</span>]
|
|
[else <span class="synSpecial">(</span>filter pred <span class="synSpecial">(</span><span class="synStatement">cdr</span> lis<span class="synSpecial">))</span>]<span class="synSpecial">))</span>
|
|
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>for-each-number proc lis<span class="synSpecial">)</span>
|
|
<span class="synSpecial">(</span><span class="synStatement">cond</span> [<span class="synSpecial">(</span><span class="synStatement">null</span>? lis<span class="synSpecial">)</span> #f]
|
|
[else <span class="synSpecial">(</span>for-each <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>x<span class="synSpecial">)</span> <span class="synSpecial">(</span>proc x<span class="synSpecial">))</span> <span class="synSpecial">(</span>my_filter <span class="synStatement">number</span>? lis<span class="synSpecial">))</span>]<span class="synSpecial">))</span>
|
|
</pre>
|
|
|
|
<p>
|
|
実行結果:
|
|
</p>
|
|
|
|
<pre class="syntax-highlight">
|
|
gosh> <span class="synSpecial">(</span>for-each-number <span class="synStatement">print</span> <span class="synSpecial">'(</span>#<span class="synStatement">t</span> 1 2 #f 3 5 6 7 8 9<span class="synSpecial">))</span>
|
|
<span class="synConstant">1</span>
|
|
<span class="synConstant">2</span>
|
|
<span class="synConstant">3</span>
|
|
<span class="synConstant">5</span>
|
|
<span class="synConstant">6</span>
|
|
<span class="synConstant">7</span>
|
|
<span class="synConstant">8</span>
|
|
<span class="synConstant">9</span>
|
|
#<undef>
|
|
gosh> <span class="synSpecial">(</span>for-each-number <span class="synStatement">print</span> <span class="synSpecial">'())</span>
|
|
#f
|
|
</pre>
|
|
|
|
<h4>
|
|
map-numbers
|
|
</h4>
|
|
|
|
<blockquote>
|
|
<p>
|
|
同様に、数値だけに手続きを適用してその結果をリストにするmap-numbersも書いてみましょう。
|
|
</p>
|
|
</blockquote>
|
|
|
|
<pre class="syntax-highlight">
|
|
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>map-numbers proc lis<span class="synSpecial">)</span>
|
|
<span class="synSpecial">(</span><span class="synStatement">cond</span> [<span class="synSpecial">(</span><span class="synStatement">null</span>? lis<span class="synSpecial">)</span> #f]
|
|
[else <span class="synSpecial">(</span><span class="synStatement">map</span> proc <span class="synSpecial">(</span>filter <span class="synStatement">number</span>? lis<span class="synSpecial">))</span>]<span class="synSpecial">))</span>
|
|
</pre>
|
|
|
|
<p>
|
|
実行結果:
|
|
</p>
|
|
|
|
<pre class="syntax-highlight">
|
|
gosh> <span class="synSpecial">(</span>map-numbers <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>x<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">*</span> x <span class="synConstant">2</span><span class="synSpecial">))</span> <span class="synSpecial">'(</span>#<span class="synStatement">t</span> 1 2 3 4 5 #f 6 7 8 9<span class="synSpecial">))</span>
|
|
<span class="synSpecial">(</span><span class="synConstant">2</span> <span class="synConstant">4</span> <span class="synConstant">6</span> <span class="synConstant">8</span> <span class="synConstant">10</span> <span class="synConstant">12</span> <span class="synConstant">14</span> <span class="synConstant">16</span> <span class="synConstant">18</span><span class="synSpecial">)</span>
|
|
</pre>
|
|
|
|
<h4>
|
|
numbers-only
|
|
</h4>
|
|
|
|
<blockquote>
|
|
<p>
|
|
for-eachやmapなどの手続きを受け取り、それを数値だけに適用する手続きへと変換するnumbers-onlyを書いてみましょう。
|
|
</p>
|
|
</blockquote>
|
|
|
|
<pre class="syntax-highlight">
|
|
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>numbers-only proc<span class="synSpecial">)</span>
|
|
<span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>proc_2 lis<span class="synSpecial">)</span>
|
|
<span class="synSpecial">(</span><span class="synStatement">cond</span> [<span class="synSpecial">(</span><span class="synStatement">null</span>? lis<span class="synSpecial">)</span> #f]
|
|
[else <span class="synSpecial">(</span>proc proc_2 <span class="synSpecial">(</span>filter <span class="synStatement">number</span>? lis<span class="synSpecial">))</span>]<span class="synSpecial">)))</span>
|
|
</pre>
|
|
|
|
<p>
|
|
実行結果:
|
|
</p>
|
|
|
|
<pre class="syntax-highlight">
|
|
gosh> <span class="synSpecial">((</span>numbers-only for-each<span class="synSpecial">)</span> <span class="synStatement">print</span> <span class="synSpecial">'(</span>#<span class="synStatement">t</span> 1 2 3 4 5<span class="synSpecial">))</span>
|
|
<span class="synConstant">1</span>
|
|
<span class="synConstant">2</span>
|
|
<span class="synConstant">3</span>
|
|
<span class="synConstant">4</span>
|
|
<span class="synConstant">5</span>
|
|
#<undef>
|
|
gosh> <span class="synSpecial">((</span>numbers-only <span class="synStatement">map</span><span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>x<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">*</span> x <span class="synConstant">2</span><span class="synSpecial">))</span> <span class="synSpecial">'(</span>#<span class="synStatement">t</span> 1 2 3 4 5<span class="synSpecial">))</span>
|
|
<span class="synSpecial">(</span><span class="synConstant">2</span> <span class="synConstant">4</span> <span class="synConstant">6</span> <span class="synConstant">8</span> <span class="synConstant">10</span><span class="synSpecial">)</span>
|
|
</pre>
|
|
</div> |