blog/content/post/2008/09/30/2008-09-30-00001041.md

99 lines
8.4 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: 自分でmaxを定義する汚い実装
author: kazu634
date: 2008-09-30
url: /2008/09/30/_1118/
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:4315;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- gauche
- Lisp
---
<div class="section">
<p>
<a href="http://d.hatena.ne.jp/SaitoAtsushi/" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/SaitoAtsushi/', 'id:SaitoAtsushi');">id:SaitoAtsushi</a>さんからせっかく教えてもらうも、applyをつかったうまい方法は思いつかず。。。とりあえずやっつけの汚い実装をする。問題は再帰で二回目以降が呼び出されるときに、引数が「<span class="footnote"><a href="/sirocco634/#f1" name="fn1" title="1 2 3 4 5">*1</a></span>」みたいになることが問題(それ以外の場合はあり得ない)のだから、
</p>
<ul>
<li>
リストの要素が1
</li>
<li>
(car リスト)がリストである
</li>
</ul>
<p>
場合に、(car リスト)を返す手続きを作ってあげればとりあえずは解決する。つまり:
</p>
<p>
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>select lis<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">and</span> <span class="synSpecial">(</span><span class="synStatement">=</span> <span class="synConstant">1</span> <span class="synSpecial">(</span>my_length lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">list</span>? <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)))</span>
<span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span>
lis<span class="synSpecial">))</span>
</pre>
<p>
このような手続きだ。これをふまえた上で定義するとこうなった:
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>my_max<span class="synStatement"> . </span>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]
[<span class="synSpecial">(</span><span class="synStatement">=</span> <span class="synSpecial">(</span>my_length <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span> <span class="synConstant">1</span><span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">car</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span>]
[<span class="synSpecial">(</span><span class="synStatement">=</span> <span class="synSpecial">(</span>my_length <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span> <span class="synConstant">2</span><span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cond</span> [<span class="synSpecial">(</span><span class="synStatement">&#60;=</span> <span class="synSpecial">(</span><span class="synStatement">car</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">)))</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span>]
[<span class="synSpecial">(</span><span class="synStatement">&#62;=</span> <span class="synSpecial">(</span><span class="synStatement">car</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">)))</span> <span class="synSpecial">(</span><span class="synStatement">car</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span>]<span class="synSpecial">)</span>]
[<span class="synSpecial">(</span><span class="synStatement">&#62;=</span> <span class="synSpecial">(</span>my_length <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span> <span class="synConstant">3</span><span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cond</span> [<span class="synSpecial">(</span><span class="synStatement">&#60;=</span> <span class="synSpecial">(</span><span class="synStatement">car</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">)))</span> <span class="synSpecial">(</span>my_max <span class="synSpecial">(</span><span class="synStatement">cons</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">cddr</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">))))</span>]
[<span class="synSpecial">(</span><span class="synStatement">&#62;=</span> <span class="synSpecial">(</span><span class="synStatement">car</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">)))</span> <span class="synSpecial">(</span>my_max <span class="synSpecial">(</span><span class="synStatement">cons</span> <span class="synSpecial">(</span><span class="synStatement">car</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">cddr</span> <span class="synSpecial">(</span>select lis<span class="synSpecial">))))</span>]<span class="synSpecial">)</span>]<span class="synSpecial">))</span>
</pre>
<p>
なんだかこれは面倒だ。絶対にもっと簡単な書き方があるような気がする。
</p>
<h4>
追記:
</h4>
<p>
ふと振り返ってみたら、<a href="http://d.hatena.ne.jp/asin/4873113482" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4873113482', 'プログラミングGauche');">プログラミングGauche</a>でpick-greater, max-numberというのが書いてあった。fold手続きを使っている。可変長引数はリストになるからそれをそのまま渡せばいいのかな
</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>
</pre>
<p>
もしかしたら、可変長引数というのは結局リストになるのだから、「リストの中で一番大きい数を探す手続き」が書ければ、「可変長引数の中で一番大きな数を探す手続き」を書いたことになるのかな?
</p>
<p>
</p>
<h4>
追記の追記:
</h4>
<p>
apply使ってないや。。。
</p>
</div>
<div class="footnote">
<p class="footnote">
<a href="/sirocco634/#fn1" name="f1">*1</a>1 2 3 4 5
</p>
</div>