blog/content/post/2008-08-28-00001013.md

82 lines
5.9 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: 手続き中の引数の書き方がわからないかもしれない
author: kazu634
date: 2008-08-28
url: /2008/08/28/_1089/
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:4239;}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/asin/4873113482" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4873113482', 'プログラミングGauche');">プログラミングGauche</a>』を再度読み直しているところ。わかっていないところが多い。とりあえず手続き中の引数の書き方はオーソドックスなのは次の二つのパターン:
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>define plus2_1
<span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>arg<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">+</span> arg <span class="synConstant">2</span><span class="synSpecial">)))</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>plus2_2 arg<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">+</span> arg <span class="synConstant">2</span><span class="synSpecial">))</span>
</pre>
<p>
こう宣言することで
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>plus2_1 <span class="synConstant">5</span><span class="synSpecial">)</span>
<span class="synConstant">7</span>
gosh&#62; <span class="synSpecial">(</span>plus2_2 <span class="synConstant">5</span><span class="synSpecial">)</span>
<span class="synConstant">7</span>
</pre>
<p>
となる。ここで76-7ページあたりの記述を見てみる。「受け取った引数を出力してから、任意の手続きを実行する手続き」としてmake-loggerが紹介されている。
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>make_logger func<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">lambda</span> args
<span class="synSpecial">(</span><span class="synStatement">print</span> <span class="synConstant">&#34;args=&#34;</span> args<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">apply</span> func args<span class="synSpecial">)))</span>
<span class="synSpecial">(</span>define my_append_log <span class="synSpecial">(</span>make_logger <span class="synStatement">append</span><span class="synSpecial">))</span>
</pre>
<p>
自分の理解だとこのmake_loggerは「(make_logger 手続き名 引数」とかが十分通用しそうに思っていた。けど、「引数は一つだ!」と怒られる。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>make_logger <span class="synStatement">print</span> <span class="synSpecial">'(</span>1 2 3<span class="synSpecial">))</span>
<span class="synStatement">***</span> ERROR: wrong <span class="synStatement">number</span> of arguments for #&#60;closure make_logger&#62; <span class="synSpecial">(</span>required <span class="synConstant">1</span>, got <span class="synConstant">2</span><span class="synSpecial">)</span>
Stack Trace:
_______________________________________
gosh&#62; <span class="synSpecial">(</span>make_logger <span class="synStatement">append</span> <span class="synSpecial">'(</span>1 2 3<span class="synSpecial">)</span> <span class="synSpecial">'(</span>1 2 3<span class="synSpecial">))</span>
<span class="synStatement">***</span> ERROR: wrong <span class="synStatement">number</span> of arguments for #&#60;closure make_logger&#62; <span class="synSpecial">(</span>required <span class="synConstant">1</span>, got <span class="synConstant">3</span><span class="synSpecial">)</span>
Stack Trace:
_______________________________________
gosh&#62; <span class="synSpecial">(</span>make_logger <span class="synStatement">append</span><span class="synSpecial">)</span>
#&#60;closure <span class="synSpecial">(</span>make_logger make_logger<span class="synSpecial">)</span><span class="synStatement">&#62;</span>
gosh&#62; <span class="synSpecial">(</span>make_logger<span class="synSpecial">)</span>
<span class="synStatement">***</span> ERROR: wrong <span class="synStatement">number</span> of arguments for #&#60;closure make_logger&#62; <span class="synSpecial">(</span>required <span class="synConstant">1</span>, got <span class="synConstant"></span><span class="synSpecial">)</span>
Stack Trace:
_______________________________________
</pre>
<p>
うでも、これは大丈夫。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>my_append_log <span class="synSpecial">'(</span>1 2 3<span class="synSpecial">)</span> <span class="synSpecial">'(</span>4 5 6<span class="synSpecial">))</span>
args=<span class="synSpecial">((</span><span class="synConstant">1</span> <span class="synConstant">2</span> <span class="synConstant">3</span><span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synConstant">4</span> <span class="synConstant">5</span> <span class="synConstant">6</span><span class="synSpecial">))</span>
<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> <span class="synConstant">6</span><span class="synSpecial">)</span>
</pre>
<p>
(make_logger)の宣言中のlambdaはどこに消えたのというか引数は一つだけではなんで複数個の引数がありなわけ
</p>
</div>