96 lines
7.9 KiB
Markdown
96 lines
7.9 KiB
Markdown
---
|
||
title: 可変長引数を受け取る手続きを書くときに再帰で処理する際はどうすればいいのかしら?
|
||
author: kazu634
|
||
date: 2008-09-28
|
||
url: /2008/09/28/_1117/
|
||
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:4313;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
||
categories:
|
||
- gauche
|
||
- Lisp
|
||
|
||
---
|
||
<div class="section">
|
||
<p>
|
||
自力でmaxを定義するの続き。どうしても定義できない。問題は
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>my_length lis<span class="synSpecial">)</span>
|
||
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">null</span>? lis<span class="synSpecial">)</span>
|
||
<span class="synConstant"></span>
|
||
<span class="synSpecial">(</span><span class="synStatement">+</span> <span class="synConstant">1</span> <span class="synSpecial">(</span>my_length <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>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 lis<span class="synSpecial">)</span> <span class="synConstant">1</span><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">=</span> <span class="synSpecial">(</span>my_length 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"><=</span> <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> lis<span class="synSpecial">)</span>]
|
||
[<span class="synSpecial">(</span><span class="synStatement">>=</span> <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span>]<span class="synSpecial">)</span>]
|
||
[<span class="synSpecial">(</span><span class="synStatement">>=</span> <span class="synSpecial">(</span>my_length 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"><=</span> <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> 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> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cddr</span> lis<span class="synSpecial">)))</span>]
|
||
[<span class="synSpecial">(</span><span class="synStatement">>=</span> <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> 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> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cddr</span> lis<span class="synSpecial">)))</span>]<span class="synSpecial">)</span>]<span class="synSpecial">))</span>
|
||
</pre>
|
||
|
||
<p>
|
||
としたときに(my_max (cons (cadr lis) (cddr lis)))とかの部分で引数として渡されると、結果として<span class="footnote"><a href="/sirocco634/#f1" name="fn1" title="1 2 3 4 5">*1</a></span>みたいなリストが渡されてmy_lengthの結果が1になってしまうこと。考えたのは
|
||
</p>
|
||
|
||
<ul>
|
||
<li>
|
||
my_lengthを拡張して、'(1 2 (3 4) (5 6))とかでも認識できるようにする
|
||
</li>
|
||
</ul>
|
||
|
||
<p>
|
||
でも、for-eachを使ってみればいいのかなーと思ったんだけど、うまく再帰の形で要素数をカウントできず。。。しょうがないから、
|
||
</p>
|
||
|
||
<ul>
|
||
<li>
|
||
‘(1 2 (3 4) (5 6))を'(1 2 3 4 5 6)にする
|
||
</li>
|
||
</ul>
|
||
|
||
<p>
|
||
を考えているところ。
|
||
</p>
|
||
|
||
<div class="hatena-asin-detail">
|
||
<a href="http://www.amazon.co.jp/dp/4873113482/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4873113482/?tag=hatena_st1-22&ascsubtag=d-7ibv', '');"><img src="https://images-na.ssl-images-amazon.com/images/I/51Exg14b4uL._SL160_.jpg" class="hatena-asin-detail-image" alt="プログラミングGauche" title="プログラミングGauche" /></a></p>
|
||
|
||
<div class="hatena-asin-detail-info">
|
||
<p class="hatena-asin-detail-title">
|
||
<a href="http://www.amazon.co.jp/dp/4873113482/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4873113482/?tag=hatena_st1-22&ascsubtag=d-7ibv', 'プログラミングGauche');">プログラミングGauche</a>
|
||
</p>
|
||
|
||
<ul>
|
||
<li>
|
||
<span class="hatena-asin-detail-label">作者:</span> <a href="http://d.hatena.ne.jp/keyword/Kahua%A5%D7%A5%ED%A5%B8%A5%A7%A5%AF%A5%C8" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/Kahua%A5%D7%A5%ED%A5%B8%A5%A7%A5%AF%A5%C8', 'Kahuaプロジェクト');" class="keyword">Kahuaプロジェクト</a>,<a href="http://d.hatena.ne.jp/keyword/%C0%EE%B9%E7%BB%CB%CF%AF" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%C0%EE%B9%E7%BB%CB%CF%AF', '川合史朗');" class="keyword">川合史朗</a>
|
||
</li>
|
||
<li>
|
||
<span class="hatena-asin-detail-label">出版社/メーカー:</span> <a href="http://d.hatena.ne.jp/keyword/%A5%AA%A5%E9%A5%A4%A5%EA%A1%BC%A5%B8%A5%E3%A5%D1%A5%F3" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%A5%AA%A5%E9%A5%A4%A5%EA%A1%BC%A5%B8%A5%E3%A5%D1%A5%F3', 'オライリージャパン');" class="keyword">オライリージャパン</a>
|
||
</li>
|
||
<li>
|
||
<span class="hatena-asin-detail-label">発売日:</span> 2008/03/14
|
||
</li>
|
||
<li>
|
||
<span class="hatena-asin-detail-label">メディア:</span> 大型本
|
||
</li>
|
||
<li>
|
||
<span class="hatena-asin-detail-label">購入</span>: 22人 <span class="hatena-asin-detail-label">クリック</span>: 713回
|
||
</li>
|
||
<li>
|
||
<a href="http://d.hatena.ne.jp/asin/4873113482" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4873113482', 'この商品を含むブログ (272件) を見る');" target="_blank">この商品を含むブログ (272件) を見る</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="hatena-asin-detail-foot">
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="footnote">
|
||
<p class="footnote">
|
||
<a href="/sirocco634/#fn1" name="f1">*1</a>:1 2 3 4 5
|
||
</p>
|
||
</div>
|