blog/content/post/2008/10/09/2008-10-09-00001049.md

78 lines
5.3 KiB
Markdown

---
title: 高階関数の練習をするよ
author: kazu634
date: 2008-10-09
url: /2008/10/09/_1126/
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:4329;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- gauche
- Lisp
---
<div class="section">
<p>
ちょっと前に自分なりにmaxを定義してみました。同じようにすれば、minも定義できます。
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>max-number lis<span class="synSpecial">)</span>
<span class="synSpecial">(</span>fold
<span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>a b<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">&#60;</span> a b<span class="synSpecial">)</span>
b
a<span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cdr</span> lis<span class="synSpecial">)))</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>my_max<span class="synStatement"> . </span>args<span class="synSpecial">)</span>
<span class="synSpecial">(</span>max-number args<span class="synSpecial">))</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>min-number lis<span class="synSpecial">)</span>
<span class="synSpecial">(</span>fold
<span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>a b<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">&#62;</span> a b<span class="synSpecial">)</span>
b
a<span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cdr</span> lis<span class="synSpecial">)))</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>my_min<span class="synStatement"> . </span>args<span class="synSpecial">)</span>
<span class="synSpecial">(</span>min-number args<span class="synSpecial">))</span>
</pre>
<p>
この中から共通の構造を抜き出して、中間的な手続きを作成してみます。この中間的な手続きは「<」や「>」を受け取って、「引数を一つ取る手続き」を戻り値とします。
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>pickup proc<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>lis<span class="synSpecial">)</span>
<span class="synSpecial">(</span>fold
<span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>a b<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span>proc a b<span class="synSpecial">)</span>
b
a<span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cdr</span> lis<span class="synSpecial">))))</span>
</pre>
<p>
これを使うと、
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>define <span class="synSpecial">(</span>my_max<span class="synStatement"> . </span>args<span class="synSpecial">)</span>
<span class="synSpecial">((</span>pickup <span class="synStatement">&#60;</span><span class="synSpecial">)</span> args<span class="synSpecial">))</span>
my_max
gosh&#62; <span class="synSpecial">(</span>my_max <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> <span class="synConstant">6</span> <span class="synConstant">7</span><span class="synSpecial">)</span>
<span class="synConstant">7</span>
gosh&#62; <span class="synSpecial">(</span>define <span class="synSpecial">(</span>my_min<span class="synStatement"> . </span>args<span class="synSpecial">)</span>
<span class="synSpecial">((</span>pickup <span class="synStatement">&#62;</span><span class="synSpecial">)</span> args<span class="synSpecial">))</span>
my_min
gosh&#62; <span class="synSpecial">(</span>my_min <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> <span class="synConstant">6</span> <span class="synConstant"></span> <span class="synConstant">34</span><span class="synSpecial">)</span>
<span class="synConstant"></span>
</pre>
<p>
というような書き方になります。
</p>
<p>
感想: だいぶ理解してきた気がする。
</p>
</div>