blog/content/post/2008/10/02/2008-10-02-00001044.md

102 lines
6.5 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: applyの使い方がわかったかもしれない
author: kazu634
date: 2008-10-02
url: /2008/10/02/_1121/
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:4321;}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>さんとShiroさんに教えていただいて、なんとなくわかった瞬間がきましたとりあえずその全過程をご覧ください。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">cons</span> <span class="synConstant">1</span> <span class="synConstant">2</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synConstant">1</span><span class="synStatement"> . </span><span class="synConstant">2</span><span class="synSpecial">)</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">apply</span> <span class="synStatement">cons</span> <span class="synSpecial">'(</span>1 2<span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synConstant">1</span><span class="synStatement"> . </span><span class="synConstant">2</span><span class="synSpecial">)</span>
gosh&#62; <span class="synSpecial">(</span>define p <span class="synSpecial">'(</span>a b c<span class="synSpecial">))</span>
p
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">apply</span> <span class="synStatement">car</span> <span class="synSpecial">(</span><span class="synStatement">list</span> p<span class="synSpecial">))</span>
a
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">car</span> p<span class="synSpecial">)</span>
a
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">car</span> <span class="synSpecial">(</span><span class="synStatement">list</span> p<span class="synSpecial">))</span>
<span class="synSpecial">(</span>a b c<span class="synSpecial">)</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">apply</span> <span class="synStatement">cdr</span> <span class="synSpecial">(</span><span class="synStatement">list</span> p<span class="synSpecial">))</span>
<span class="synSpecial">(</span>b c<span class="synSpecial">)</span>
</pre>
<p>
ここら辺の段階ではまだよくわかっていない。。。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">apply</span> list-ref <span class="synSpecial">(</span><span class="synStatement">list</span> p <span class="synConstant"></span><span class="synSpecial">))</span>
a
gosh&#62; <span class="synSpecial">(</span>list-ref p <span class="synConstant"></span><span class="synSpecial">)</span>
a
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">apply</span> list-ref <span class="synSpecial">(</span><span class="synStatement">list</span> p <span class="synConstant">1</span><span class="synSpecial">))</span>
b
gosh&#62; <span class="synSpecial">(</span>list-ref p <span class="synConstant">1</span><span class="synSpecial">)</span>
b
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">apply</span> list-ref <span class="synSpecial">(</span><span class="synStatement">list</span> p <span class="synConstant">2</span><span class="synSpecial">))</span>
c
gosh&#62; <span class="synSpecial">(</span>list-ref p <span class="synConstant">2</span><span class="synSpecial">)</span>
c
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">apply</span> <span class="synStatement">length</span> <span class="synSpecial">(</span><span class="synStatement">list</span> p<span class="synSpecial">))</span>
<span class="synConstant">3</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">length</span> p<span class="synSpecial">)</span>
<span class="synConstant">3</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">apply</span> list-ref <span class="synSpecial">'(</span>1<span class="synSpecial">))</span>
<span class="synStatement">***</span> ERROR: wrong <span class="synStatement">number</span> of arguments for #&#60;subr list-ref&#62; <span class="synSpecial">(</span>required <span class="synConstant">2</span>, got <span class="synConstant">1</span><span class="synSpecial">)</span>
Stack Trace:
_______________________________________
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">list</span> p <span class="synConstant">2</span><span class="synSpecial">)</span>
<span class="synSpecial">((</span>a b c<span class="synSpecial">)</span> <span class="synConstant">2</span><span class="synSpecial">)</span>
</pre>
<p>
ここら辺でもまだ事の重要性に気づいていなかった。。。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>list-ref p <span class="synConstant">1</span><span class="synSpecial">)</span>
b
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">list</span> p <span class="synConstant"></span><span class="synSpecial">)</span>
<span class="synSpecial">((</span>a b c<span class="synSpecial">)</span> <span class="synConstant"></span><span class="synSpecial">)</span>
</pre>
<p>
もしや…applyって「一番外側の()を外して手続きに引数として渡している」んじゃあ…そうじゃないと、
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">apply</span> list-ref <span class="synSpecial">(</span><span class="synStatement">list</span> p <span class="synConstant">2</span><span class="synSpecial">))</span>
c
</pre>
<p>
とかになる理由が理解できない。これは結局
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>list-ref <span class="synSpecial">'(</span>a b c<span class="synSpecial">)</span> <span class="synConstant"></span><span class="synSpecial">)</span>
</pre>
<p>
となっているはず。そうじゃなければ、こんな挙動になるはずがない。。。
</p>
<p>
</p>
<p>
Shiro様、ありがとうございました(__)理解できました。
</p>
</div>