blog/content/post/2008/08/04/2008-08-04-00000989.md

99 lines
6.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 等価性
author: kazu634
date: 2008-08-04
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:4187;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- gauche
- Lisp
---
<div class="section">
<p>
「二つのオブジェクトAとBが等しいとはどういうことでしょうか」これには色々な意味があるよ。
</p>
<h4>
内容が同じという場合
</h4>
<p>
二つのリストがあった場合、それぞれの要素を順に比較し、全て等しければ元のリストも等しいという立場での比較。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">equal</span>? <span class="synSpecial">(</span><span class="synStatement">list</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="synStatement">list</span> <span class="synConstant">1</span> <span class="synConstant">2</span> <span class="synConstant">3</span><span class="synSpecial">))</span>
#<span class="synStatement">t</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">equal</span>? <span class="synSpecial">(</span><span class="synStatement">list</span> <span class="synConstant">1</span> <span class="synConstant">2</span> <span class="synSpecial">(</span><span class="synStatement">list</span> <span class="synConstant">3</span> <span class="synConstant">4</span><span class="synSpecial">)</span> <span class="synConstant">5</span><span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cons</span> <span class="synConstant">1</span> <span class="synSpecial">(</span><span class="synStatement">cons</span> <span class="synConstant">2</span> <span class="synSpecial">'((</span>3 4<span class="synSpecial">)</span> . <span class="synSpecial">(</span>5<span class="synSpecial">)))))</span>
#<span class="synStatement">t</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">equal</span>? <span class="synSpecial">(</span><span class="synStatement">list</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="synStatement">list</span> <span class="synConstant">1</span> <span class="synConstant">2</span> <span class="synConstant">4</span><span class="synSpecial">))</span>
#f
</pre>
<h4>
入れ物が同じ
</h4>
<p>
他の値を入れておく入れ物の同一性を調べるもの。C言語を知っている人は、eq?はオブジェクトの「アドレス」を比較していると考えていい。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">eq</span>? <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="synStatement">cons</span> <span class="synConstant">1</span> <span class="synConstant">2</span><span class="synSpecial">))</span>
#f
gosh&#62; <span class="synSpecial">(</span>define p <span class="synSpecial">(</span><span class="synStatement">cons</span> <span class="synConstant">1</span> <span class="synConstant">2</span><span class="synSpecial">))</span>
p
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">eq</span>? p p<span class="synSpecial">)</span>
#<span class="synStatement">t</span>
</pre>
<h4>
数値として同じ
</h4>
<p>
数値として比較する場合。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>eqv? <span class="synConstant">1</span> <span class="synConstant">1</span><span class="synSpecial">)</span>
#<span class="synStatement">t</span>
gosh&#62; <span class="synSpecial">(</span>eqv? <span class="synConstant">1.0</span> <span class="synConstant">1.0</span><span class="synSpecial">)</span>
#<span class="synStatement">t</span>
gosh&#62; <span class="synSpecial">(</span>eqv? <span class="synConstant">1</span> <span class="synConstant">1.0</span><span class="synSpecial">)</span>
#f
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">=</span> <span class="synConstant">1</span> <span class="synConstant">1.0</span><span class="synSpecial">)</span>
#<span class="synStatement">t</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">char=</span>? #\a #\a<span class="synSpecial">)</span>
#<span class="synStatement">t</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">char=</span>? #\a <span class="synType">#\A</span><span class="synSpecial">)</span>
#f
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">string=</span>? <span class="synConstant">&#34;abc&#34;</span> <span class="synConstant">&#34;abc&#34;</span><span class="synSpecial">)</span>
#<span class="synStatement">t</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">string=</span>? <span class="synConstant">&#34;abc&#34;</span> <span class="synConstant">&#34;ABC&#34;</span><span class="synSpecial">)</span>
#f
</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>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080727/1217160745" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080727/1217160745', ' Hello world! &#8211; 武蔵の日記');" target="_blank"> Hello world! &#8211; 武蔵の日記</a>
</li>
</ul>
</li>
</ul>
</blockquote>
</div>