blog/content/post/2008/08/05/2008-08-05-00000991.md

210 lines
8.0 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: '8章: 真偽値と判断'
author: kazu634
date: 2008-08-05
url: /2008/08/05/_1066/
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:4191;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- gauche
- Lisp
---
<div class="section">
<p>
勉強の続きだよ。
</p>
<p>
<a name="seemore"></a>
</p>
<h4>
型を判定する手続き
</h4>
<ul>
<li>
pair?
</li>
<li>
null?
</li>
<li>
list?
</li>
<li>
boolean?
</li>
<li>
symbol?
</li>
<li>
number?
</li>
<li>
char?
</li>
<li>
string?
</li>
</ul>
<h4>
性質を判定する手続き
</h4>
<ul>
<li>
odd?
</li>
<li>
even?
</li>
<li>
zero?
</li>
<li>
exact?
</li>
<li>
char-alphabetic?
</li>
<li>
proper-list?
</li>
</ul>
<h4>
not
</h4>
<p>
否定だよ。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">not</span> <span class="synSpecial">(</span><span class="synStatement">=</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">not</span> <span class="synSpecial">(</span><span class="synStatement">=</span> <span class="synConstant">2</span> <span class="synConstant">2</span><span class="synSpecial">))</span>
#f
</pre>
<h4>
条件を判断するcase手続き
</h4>
<p>
select caseなんかと同じみたいだ。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>define <span class="synSpecial">(</span>score card<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">case</span> card
<span class="synSpecial">((</span>A<span class="synSpecial">)</span> <span class="synConstant">11</span><span class="synSpecial">)</span>
<span class="synSpecial">((</span>J Q K<span class="synSpecial">)</span> <span class="synConstant">10</span><span class="synSpecial">)</span>
<span class="synSpecial">((</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="synConstant">7</span> <span class="synConstant">8</span> <span class="synConstant">9</span> <span class="synConstant">10</span><span class="synSpecial">)</span> card<span class="synSpecial">)))</span>
score
gosh&#62; <span class="synSpecial">(</span>score <span class="synConstant">4</span><span class="synSpecial">)</span>
<span class="synConstant">4</span>
gosh&#62; <span class="synSpecial">(</span>score <span class="synConstant">10</span><span class="synSpecial">)</span>
<span class="synConstant">10</span>
gosh&#62; <span class="synSpecial">(</span>score <span class="synSpecial">'</span><span class="synIdentifier">A</span><span class="synSpecial">)</span>
<span class="synConstant">11</span>
</pre>
<h4>
条件が真の時だけ処理を行うwhen手続き
</h4>
<p>
これだとif手続きの方がいいのかもしれないけど:
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>define <span class="synSpecial">(</span>foo num<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">when</span> <span class="synSpecial">(</span><span class="synStatement">&#60;</span> num <span class="synConstant">10</span><span class="synSpecial">)</span>
num<span class="synSpecial">))</span>
foo
gosh&#62; <span class="synSpecial">(</span>foo <span class="synConstant">4</span><span class="synSpecial">)</span>
<span class="synConstant">4</span>
gosh&#62; <span class="synSpecial">(</span>foo <span class="synConstant">11</span><span class="synSpecial">)</span>
#&#60;undef&#62;
</pre>
<p>
ちなみに条件式が偽のときだけ式を評価させたい場合はunlessを用いるよん。
</p>
<h4>
1から任意の整数までの和を足しあわせた結果を表示
</h4>
<p>
こんなんでいいのかー。すごく短い。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>define <span class="synSpecial">(</span>sum 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">1</span><span class="synSpecial">)</span>
<span class="synConstant">1</span>
<span class="synSpecial">(</span><span class="synStatement">+</span> <span class="synSpecial">(</span>sum <span class="synSpecial">(</span><span class="synStatement">-</span> n <span class="synConstant">1</span><span class="synSpecial">))</span> n<span class="synSpecial">)))</span>
sum
gosh&#62; <span class="synSpecial">(</span>sum <span class="synConstant">10</span><span class="synSpecial">)</span>
<span class="synConstant">55</span>
</pre>
<p>
Cだとこんな感じかしら
</p>
<pre class="syntax-highlight">
<span class="synComment">/* ======================= */</span>
<span class="synComment">/* === Include library === */</span>
<span class="synComment">/* ======================= */</span>
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdlib.h&#62;</span>
<span class="synComment">/* ============ */</span>
<span class="synComment">/* === main === */</span>
<span class="synComment">/* ============ */</span>
<span class="synType">int</span> main(<span class="synType">int</span> argc, <span class="synType">char</span> *argv[])
{
<span class="synType">int</span> i = <span class="synConstant"></span>;
<span class="synType">int</span> sum = <span class="synConstant"></span>;
<span class="synType">int</span> num = atoi(argv[<span class="synConstant">1</span>]);
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; i &#60;= num; ++i)
{
sum += i;
}
printf(<span class="synConstant">&#34;</span><span class="synSpecial">%d\n</span><span class="synConstant">&#34;</span>, sum);
}
</pre>
<p>
発想が全然違う気がする。
</p>
<blockquote>
<ul>
<li>
「gauche」に関連する最近のエントリ <ul>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080804/1217853753" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080804/1217853753', ' 等価性 &#8211; 武蔵の日記');" target="_blank"> 等価性 &#8211; 武蔵の日記</a>
</li>
<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>