blog/content/post/2008/07/27/2008-07-27-00000979.md

259 lines
13 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-07-27
url: /2008/07/27/_1053/
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:4165;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- gauche
- Lisp
---
<div class="section">
<p>
淡々と書いてみるよ。
</p>
<h4>
Schemeの言語仕様
</h4>
<blockquote>
<p>
それが重要な結果を招くとわかった上で再定義することをSchemeの言語仕様は妨げません。すべてユーザの自由にゆだねられています。
</p>
</blockquote>
<h4>
append手続き
</h4>
<p>
リストとリストを結合させる。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">append</span> <span class="synSpecial">'(</span>1 1 2<span class="synSpecial">)</span> <span class="synSpecial">'(</span>2 2 3<span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synConstant">1</span> <span class="synConstant">1</span> <span class="synConstant">2</span> <span class="synConstant">2</span> <span class="synConstant">2</span> <span class="synConstant">3</span><span class="synSpecial">)</span>
</pre>
<h4>
car手続き
</h4>
<p>
リストの先頭要素を返します:
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">car</span> <span class="synSpecial">'(</span>1 2 3<span class="synSpecial">))</span>
<span class="synConstant">1</span>
</pre>
<h4>
cdr手続き
</h4>
<p>
リストの先頭をのぞいた残りの要素を返します。
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">cdr</span> <span class="synSpecial">'(</span>1 2 3<span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synConstant">2</span> <span class="synConstant">3</span><span class="synSpecial">)</span>
</pre>
<h4>
cons手続き
</h4>
<p>
先頭要素と残りの要素からなるリストをくっつけてリストにする:
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">cons</span> <span class="synConstant">1</span> <span class="synSpecial">'(</span>2 3<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="synSpecial">)</span>
</pre>
<h4>
list手続き
</h4>
<p>
リストを作る手続き:
</p>
<pre class="syntax-highlight">
gosh&#62; <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="synConstant">4</span> <span class="synConstant">5</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="synSpecial">)</span>
</pre>
<h4>
null?手続き
</h4>
<p>
空リストかどうかを判断する手続き:
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">null</span>? <span class="synSpecial">'(</span>1 2 3<span class="synSpecial">))</span>
#f
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">null</span>? <span class="synSpecial">'())</span>
#<span class="synStatement">t</span>
</pre>
<h4>
pair?手続き
</h4>
<p>
空リストではないかどうかを判断する手続き:
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>pair? <span class="synSpecial">'(</span>1 2 3<span class="synSpecial">))</span>
#<span class="synStatement">t</span>
</pre>
<h4>
fold手続き
</h4>
<p>
リストの要素を順に処理していく手続きの基本になる手続き:
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>fold <span class="synStatement">+</span> <span class="synConstant"></span> <span class="synSpecial">'(</span>1 2 3 4 5 6 7 8 9 10<span class="synSpecial">))</span>
<span class="synConstant">55</span>
</pre>
<p>
やっていることはperlのforeachみたいな感じなのかな
</p>
<pre class="syntax-highlight">
<span class="synComment"># === use ===</span>
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synComment"># === main ===</span>
<span class="synStatement">my</span> <span class="synIdentifier">@array</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="synConstant">7</span>, <span class="synConstant">8</span>, <span class="synConstant">9</span>, <span class="synConstant">10</span>);
<span class="synStatement">my</span> <span class="synIdentifier">$sum</span> = <span class="synConstant"></span>;
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$i</span> (<span class="synIdentifier">@array</span>) {
<span class="synIdentifier">$sum</span> += <span class="synIdentifier">$i</span>;
}
<span class="synStatement">print</span> <span class="synIdentifier">$sum</span>, <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</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="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> array[<span class="synConstant">10</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="synConstant">7</span>, <span class="synConstant">8</span>, <span class="synConstant">9</span>, <span class="synConstant">10</span>};
<span class="synType">int</span> sum = <span class="synConstant"></span>;
<span class="synType">int</span> i;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; i &#60; (<span class="synStatement">sizeof</span>(array) / <span class="synStatement">sizeof</span>(<span class="synType">int</span>));i++)
{
sum += array[i];
}
printf(<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, sum);
<span class="synStatement">return</span> <span class="synConstant"></span>;
}
</pre>
<h4>
foldの練習
</h4>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>define <span class="synSpecial">(</span>pick-least a b<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">&#60;</span> a b<span class="synSpecial">)</span> a b<span class="synSpecial">))</span>
gosh&#62; <span class="synSpecial">(</span>define <span class="synSpecial">(</span>min-number lis<span class="synSpecial">)</span>
<span class="synSpecial">(</span>fold pick-least +inf.0 lis<span class="synSpecial">))</span>
gosh&#62; <span class="synSpecial">(</span>min-number <span class="synSpecial">'(</span>0 1 2 3 4 5 6 7 8<span class="synSpecial">))</span>
<span class="synConstant"></span>
</pre>
<p>
こんな書き方もOKらしい:
</p>
<pre class="syntax-highlight">
<span class="synStatement">&#62;</span>||<span class="synSpecial">(</span>define <span class="synSpecial">(</span>min-number lis<span class="synSpecial">)</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>pick-least a b<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">&#60;</span> a b<span class="synSpecial">)</span> a b<span class="synSpecial">))</span>
<span class="synSpecial">(</span>fold pick-least -inf.0 lis<span class="synSpecial">))</span>
</pre>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>define <span class="synSpecial">(</span><span class="synStatement">length</span> lis<span class="synSpecial">)</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>increment2 a b<span class="synSpecial">)(</span><span class="synStatement">+</span> b <span class="synConstant">1</span><span class="synSpecial">))</span>
<span class="synSpecial">(</span>fold increment2 <span class="synConstant"></span> lis<span class="synSpecial">))</span>
</pre>
<h4>
感想
</h4>
<blockquote title="How To Become A Hacker" cite="http://cruel.org/freeware/hacker.html">
<p>
LISP は、それをモノにしたときのすばらしい悟り体験のために勉強しましょう。この体験は、その後の人生でよりよいプログラマーとなる手助けとなるはずです。たとえ、実際には LISP そのものをあまり使わなくても。
</p>
<p>
<cite><a href="http://cruel.org/freeware/hacker.html" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://cruel.org/freeware/hacker.html', 'How To Become A Hacker: Japanese');" target="_blank">How To Become A Hacker: Japanese</a></cite>
</p>
</blockquote>
<p>
悟り体験…なんか言っていることがわかったかもしれな、
</p>
<div class="hatena-asin-detail">
<a href="http://www.amazon.co.jp/dp/4873113482/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4873113482/?tag=hatena_st1-22&ascsubtag=d-7ibv', '');"><img src="https://images-na.ssl-images-amazon.com/images/I/51Exg14b4uL._SL160_.jpg" class="hatena-asin-detail-image" alt="プログラミングGauche" title="プログラミングGauche" /></a></p>
<div class="hatena-asin-detail-info">
<p class="hatena-asin-detail-title">
<a href="http://www.amazon.co.jp/dp/4873113482/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4873113482/?tag=hatena_st1-22&ascsubtag=d-7ibv', 'プログラミングGauche');">プログラミングGauche</a>
</p>
<ul>
<li>
<span class="hatena-asin-detail-label">作者:</span> <a href="http://d.hatena.ne.jp/keyword/Kahua%A5%D7%A5%ED%A5%B8%A5%A7%A5%AF%A5%C8" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/Kahua%A5%D7%A5%ED%A5%B8%A5%A7%A5%AF%A5%C8', 'Kahuaプロジェクト');" class="keyword">Kahuaプロジェクト</a>,<a href="http://d.hatena.ne.jp/keyword/%C0%EE%B9%E7%BB%CB%CF%AF" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%C0%EE%B9%E7%BB%CB%CF%AF', '川合史朗');" class="keyword">川合史朗</a>
</li>
<li>
<span class="hatena-asin-detail-label">出版社/メーカー:</span> <a href="http://d.hatena.ne.jp/keyword/%A5%AA%A5%E9%A5%A4%A5%EA%A1%BC%A5%B8%A5%E3%A5%D1%A5%F3" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%A5%AA%A5%E9%A5%A4%A5%EA%A1%BC%A5%B8%A5%E3%A5%D1%A5%F3', 'オライリージャパン');" class="keyword">オライリージャパン</a>
</li>
<li>
<span class="hatena-asin-detail-label">発売日:</span> 2008/03/14
</li>
<li>
<span class="hatena-asin-detail-label">メディア:</span> 大型本
</li>
<li>
<span class="hatena-asin-detail-label">購入</span>: 22人 <span class="hatena-asin-detail-label">クリック</span>: 713回
</li>
<li>
<a href="http://d.hatena.ne.jp/asin/4873113482" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4873113482', 'この商品を含むブログ (272件) を見る');" target="_blank">この商品を含むブログ (272件) を見る</a>
</li>
</ul>
</div>
<div class="hatena-asin-detail-foot">
</div>
</div>
</div>