blog/content/post/2008-08-03-00000988.md

202 lines
13 KiB
Markdown

---
title: 複数の引数をとる手続きだよ
author: kazu634
date: 2008-08-03
url: /2008/08/03/_1062/
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:4185;}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>
複数の引数をとる手続きだよ
</h4>
<p>
こんな風に書くらしい。「. args」が「0個以上の引数リスト」を意味する。「1つ以上の引数リスト」は「a . args」となるそうな。
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>use srfi-1<span class="synSpecial">)</span>
<span class="synSpecial">(</span>use util.match<span class="synSpecial">)</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span><span class="synStatement">append . </span>args<span class="synSpecial">)</span>
<span class="synSpecial">(</span>match args
[<span class="synSpecial">()</span> <span class="synSpecial">'()</span>]
[<span class="synSpecial">(</span>a<span class="synSpecial">)</span> a]
[<span class="synSpecial">(</span>a<span class="synStatement"> . </span>b<span class="synSpecial">)</span> <span class="synSpecial">(</span>append2 a <span class="synSpecial">(</span><span class="synStatement">apply</span> <span class="synStatement">append</span> b<span class="synSpecial">))</span>]<span class="synSpecial">))</span>
</pre>
<p>
このようにしてやると、引数のチェックをして、複数の引数をとる手続きを書けるみたい。
</p>
<h4>
省略可能引数
</h4>
<p>
こんな感じー
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>define <span class="synSpecial">(</span><span class="synStatement">make-list</span> num<span class="synStatement"> . </span>args<span class="synSpecial">)</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>maker n init<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">=</span> n <span class="synConstant"></span><span class="synSpecial">)</span>
<span class="synSpecial">'()</span>
<span class="synSpecial">(</span><span class="synStatement">cons</span> init <span class="synSpecial">(</span>maker <span class="synSpecial">(</span><span class="synStatement">-</span> n <span class="synConstant">1</span><span class="synSpecial">)</span> init<span class="synSpecial">))))</span>
<span class="synSpecial">(</span>maker num <span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">null</span>? args<span class="synSpecial">)</span> #f <span class="synSpecial">(</span><span class="synStatement">car</span> args<span class="synSpecial">))))</span>
<span class="synStatement">make-list</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">make-list</span> <span class="synConstant">10</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span>#f #f #f #f #f #f #f #f #f #f<span class="synSpecial">)</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">make-list</span> <span class="synConstant">10</span> <span class="synConstant">1</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synConstant">1</span> <span class="synConstant">1</span> <span class="synConstant">1</span> <span class="synConstant">1</span> <span class="synConstant">1</span> <span class="synConstant">1</span> <span class="synConstant">1</span> <span class="synConstant">1</span> <span class="synConstant">1</span> <span class="synConstant">1</span><span class="synSpecial">)</span>
</pre>
<p>
基本的な考え方としては「可変長引数が空」という扱いにするらしい。それ以外の方法としては let-optionals*を使うらしい。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>define <span class="synSpecial">(</span><span class="synStatement">make-list</span> num<span class="synStatement"> . </span>args<span class="synSpecial">)</span>
<span class="synSpecial">(</span>let-optionals* args <span class="synSpecial">((</span>init #f<span class="synSpecial">))</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>maker n<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">=</span> n <span class="synConstant"></span><span class="synSpecial">)</span> <span class="synSpecial">'()</span> <span class="synSpecial">(</span><span class="synStatement">cons</span> init <span class="synSpecial">(</span>maker <span class="synSpecial">(</span><span class="synStatement">-</span> n <span class="synConstant">1</span><span class="synSpecial">)))))</span>
<span class="synSpecial">(</span>maker num<span class="synSpecial">)))</span>
<span class="synStatement">make-list</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">make-list</span> <span class="synConstant">1</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span>#f<span class="synSpecial">)</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">make-list</span> <span class="synConstant">1</span> <span class="synConstant">2</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synConstant">2</span><span class="synSpecial">)</span>
</pre>
<h4>
多値
</h4>
<p>
Lispは複数の返値を返すことができる。それの受け取り方や、複数の返値を返す方法を考えてみるよ。
</p>
<h4>
多値を受け取る
</h4>
<p>
receive手続きを使うよ。mix&maxは最小値と最大値を返す手続き。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>receive <span class="synSpecial">(</span>min-val max-val<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">min</span>&#38;max <span class="synConstant">3</span> <span class="synConstant">1</span> <span class="synConstant">2</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">list</span> min-val max-val<span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synConstant">1</span> <span class="synConstant">3</span><span class="synSpecial">)</span>
</pre>
<p>
二つの戻り値に対して二つの格納する変数を用意する方法だけでなく、一つのリストとして格納することもあり。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>receive all-values
<span class="synSpecial">(</span><span class="synStatement">min</span>&#38;max <span class="synConstant">3</span> <span class="synConstant">1</span> <span class="synConstant">2</span><span class="synSpecial">)</span>
all-values<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synConstant">1</span> <span class="synConstant">3</span><span class="synSpecial">)</span>
</pre>
<p>
values-refを使うと、戻り値を配列に格納したかのようにして扱うことができる。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>values-ref <span class="synSpecial">(</span><span class="synStatement">min</span>&#38;max <span class="synConstant"></span> <span class="synConstant">3</span> <span class="synConstant">-1</span><span class="synSpecial">)</span> <span class="synConstant"></span><span class="synSpecial">)</span>
<span class="synConstant">-1</span>
</pre>
<p>
これで戻り値の0番目の数字を取り出すように指示している。
</p>
<h4>
FizzBuzz
</h4>
<blockquote>
<p>
データからデータへの変換を考えればよく、出力は後からどうにでもなる。
</p>
</blockquote>
<p>
これがSchemeプログラマの基本的な発想…らしい。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>for-each <span class="synStatement">print</span>
<span class="synSpecial">(</span><span class="synStatement">map</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">cond</span> [<span class="synSpecial">(</span><span class="synStatement">=</span> <span class="synSpecial">(</span>modulo x <span class="synConstant">15</span><span class="synSpecial">)</span> <span class="synConstant"></span><span class="synSpecial">)</span> <span class="synSpecial">'</span><span class="synIdentifier">FizzBuzz]</span>
[<span class="synSpecial">(</span><span class="synStatement">=</span> <span class="synSpecial">(</span>modulo x <span class="synConstant">5</span><span class="synSpecial">)</span> <span class="synConstant"></span><span class="synSpecial">)</span> <span class="synSpecial">'</span><span class="synIdentifier">Buzz]</span>
[<span class="synSpecial">(</span><span class="synStatement">=</span> <span class="synSpecial">(</span>modulo x <span class="synConstant">3</span><span class="synSpecial">)</span> <span class="synConstant"></span><span class="synSpecial">)</span> <span class="synSpecial">'</span><span class="synIdentifier">Fizz]</span>
[else x]<span class="synSpecial">))</span>
<span class="synSpecial">(</span>iota <span class="synConstant">100</span> <span class="synConstant">1</span><span class="synSpecial">)))</span>
</pre>
<blockquote>
<ul>
<li>
「gauche」に関連する最近のエントリ <ul>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080803/1217768505" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080803/1217768505', ' 複数の引数をとる手続きだよ &#8211; 武蔵の日記');" target="_blank"> 複数の引数をとる手続きだよ &#8211; 武蔵の日記</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080802/1217685822" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080802/1217685822', ' 『プログラミングGauche』の勉強 &#8211; 武蔵の日記');" target="_blank"> 『プログラミングGauche』の勉強 &#8211; 武蔵の日記</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080729/1217342129" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080729/1217342129', ' lambdaでてきた &#8211; 武蔵の日記');" target="_blank"> lambdaでてきた &#8211; 武蔵の日記</a>
</li>
</ul>
</li>
</ul>
</blockquote>
<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>