69 lines
4.9 KiB
Markdown
69 lines
4.9 KiB
Markdown
---
|
||
title: applyの挙動
|
||
author: kazu634
|
||
date: 2008-10-01
|
||
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:4317;}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/SaitoAtushi/" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/SaitoAtushi/', 'id:SaitoAtushi');">id:SaitoAtushi</a>様から教えていただいたapplyについてのコメントを参考にしてやってみた:
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
gosh> <span class="synSpecial">(</span>define <span class="synSpecial">(</span>hoge<span class="synStatement"> . </span>args<span class="synSpecial">)</span>
|
||
<span class="synSpecial">(</span><span class="synStatement">write</span> args<span class="synSpecial">))</span>
|
||
hoge
|
||
gosh> <span class="synSpecial">(</span><span class="synStatement">apply</span> hoge <span class="synSpecial">'(</span>1 2 3 4<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="synSpecial">)</span>#<undef>
|
||
gosh> <span class="synSpecial">(</span>hoge <span class="synSpecial">'(</span>1 2 3 4<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="synSpecial">))</span>#<undef>
|
||
gosh> <span class="synSpecial">(</span>define <span class="synSpecial">(</span>hoge<span class="synStatement"> . </span>args<span class="synSpecial">)</span>
|
||
<span class="synSpecial">(</span><span class="synStatement">print</span> args<span class="synSpecial">))</span>
|
||
hoge
|
||
gosh> <span class="synSpecial">(</span>hoge <span class="synSpecial">'(</span>1 2 3 4<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="synSpecial">))</span>
|
||
#<undef>
|
||
gosh> <span class="synSpecial">(</span><span class="synStatement">apply</span> hoge <span class="synSpecial">'(</span>1 2 3 4<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="synSpecial">)</span>
|
||
#<undef>
|
||
</pre>
|
||
|
||
<p>
|
||
applyを使うときと使わないときとではこんな挙動の違いがあったのですが。。。こいつは知らなかったです。。。applyを使うとどんな挙動をしているんだろう?applyはリストの要素一つ一つを手続きに渡す…はずだけど、今回のhogeはリストを返している。ちょっと実験をしてみる。
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
gosh> <span class="synSpecial">(</span><span class="synStatement">+</span> <span class="synSpecial">'(</span>1 2 3 4<span class="synSpecial">))</span>
|
||
<span class="synStatement">***</span> ERROR: <span class="synStatement">number</span> required, but got <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="synSpecial">)</span>
|
||
Stack Trace:
|
||
_______________________________________
|
||
gosh> <span class="synSpecial">(</span><span class="synStatement">apply</span> <span class="synStatement">+</span> <span class="synSpecial">'(</span>1 2 3 4<span class="synSpecial">))</span>
|
||
<span class="synConstant">10</span>
|
||
</pre>
|
||
|
||
<p>
|
||
これから判断するに、内部的にはapplyは
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synSpecial">(</span><span class="synStatement">+</span> <span class="synSpecial">(</span><span class="synStatement">+</span> <span class="synSpecial">(</span><span class="synStatement">+</span> <span class="synSpecial">(</span><span class="synStatement">+</span> <span class="synConstant">4</span><span class="synSpecial">)</span> <span class="synConstant">3</span><span class="synSpecial">)</span> <span class="synConstant">2</span><span class="synSpecial">)</span> <span class="synConstant">1</span><span class="synSpecial">)</span>
|
||
</pre>
|
||
|
||
<p>
|
||
というような挙動をしているはず。でも、
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synSpecial">(</span><span class="synStatement">apply</span> hoge <span class="synSpecial">'(</span>1 2 3 4<span class="synSpecial">)</span>
|
||
</pre>
|
||
|
||
<p>
|
||
はリストを返す。なんかこんがらがってきたぞ。。。
|
||
</p>
|
||
</div>
|