blog/content/post/2008/10/25/2008-10-25-00001056.md

79 lines
6.2 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: 『プログラミングGauche』を読む
author: kazu634
date: 2008-10-25
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:4349;}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/asin/4873113482" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4873113482', 'プログラミングGauche');">プログラミングGauche</a>』の勉強だよ。気になった部分だけ抜粋。
</p>
<h4>
出力
</h4>
<p>
format手続きを使うよ。これはCのprintfと一緒。
</p>
<pre class="syntax-highlight">
<span class="synComment">;; formatの使い方</span>
<span class="synComment">;; &#34;~d&#34;: 10進数</span>
<span class="synComment">;; &#34;~b&#34;: 2進数</span>
<span class="synComment">;; &#34;~o&#34;: 8進数</span>
<span class="synComment">;; &#34;~x&#34;: 16進数</span>
<span class="synComment">;; ~とアルファベットの間の数字は桁数を示す</span>
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">format</span> #t <span class="synConstant">&#34;Result = |~3@a|\n&#34;</span> <span class="synSpecial">(</span><span class="synStatement">*</span> <span class="synConstant">3</span> <span class="synConstant">4</span><span class="synSpecial">))</span>
Result <span class="synStatement">=</span> | <span class="synConstant">12</span>|
#&#60;undef&#62;
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">format</span> #t <span class="synConstant">&#34;Result = |~5@a|\n&#34;</span> <span class="synSpecial">(</span><span class="synStatement">*</span> <span class="synConstant">3</span> <span class="synConstant">4</span><span class="synSpecial">))</span>
Result <span class="synStatement">=</span> | <span class="synConstant">12</span>|
#&#60;undef&#62;
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">format</span> #t <span class="synConstant">&#34;Result = |~10@a|\n&#34;</span> <span class="synSpecial">(</span><span class="synStatement">*</span> <span class="synConstant">3</span> <span class="synConstant">4</span><span class="synSpecial">))</span>
Result <span class="synStatement">=</span> | <span class="synConstant">12</span>|
#&#60;undef&#62;
</pre>
<h4>
ハッシュ
</h4>
<p>
ハッシュも使えるよ。Perlから影響を受けて作ったそうなので、かなり簡単に使えるようになっている気がする。
</p>
<pre class="syntax-highlight">
<span class="synComment">;; === ハッシュ ===</span>
<span class="synComment">;; ハッシュの生成</span>
<span class="synSpecial">(</span>define <span class="synStatement">hash-table</span> <span class="synSpecial">(</span><span class="synStatement">make-hash-table</span><span class="synSpecial">))</span>
<span class="synComment">;; ハッシュにデータを追加</span>
<span class="synSpecial">(</span>hash-table-put! <span class="synStatement">hash-table</span> <span class="synSpecial">'</span><span class="synIdentifier">name</span> <span class="synConstant">&#34;Gauche&#34;</span><span class="synSpecial">)</span>
<span class="synComment">;; ハッシュからデータを入手する</span>
<span class="synSpecial">(</span>hash-table-get <span class="synStatement">hash-table</span> <span class="synSpecial">'</span><span class="synIdentifier">name</span><span class="synSpecial">)</span>
<span class="synComment">;; ハッシュからデータを削除する</span>
<span class="synSpecial">(</span>hash-table-delete! <span class="synStatement">hash-table</span> <span class="synSpecial">'</span><span class="synIdentifier">name</span><span class="synSpecial">)</span>
<span class="synComment">;; ハッシュのキーに複数のデータを登録する</span>
<span class="synComment">;; nameキーに「Gauche」と「foo」をヒモづける</span>
<span class="synSpecial">(</span>hash-table-push! <span class="synStatement">hash-table</span> <span class="synSpecial">'</span><span class="synIdentifier">name</span> <span class="synConstant">&#34;Gauche&#34;</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span>hash-table-push! <span class="synStatement">hash-table</span> <span class="synSpecial">'</span><span class="synIdentifier">name</span> <span class="synConstant">&#34;foo&#34;</span><span class="synSpecial">)</span>
<span class="synComment">;; ハッシュのキーからポップする</span>
<span class="synSpecial">(</span>hash-table-pop! <span class="synStatement">hash-table</span> <span class="synSpecial">'</span><span class="synIdentifier">name</span><span class="synSpecial">)</span>
<span class="synComment">;; ハッシュのキーを列挙</span>
<span class="synSpecial">(</span>hash-table-keys <span class="synStatement">hash-table</span><span class="synSpecial">)</span>
<span class="synComment">;; ハッシュのvalueを列挙</span>
<span class="synSpecial">(</span>hash-table-values <span class="synStatement">hash-table</span><span class="synSpecial">)</span>
<span class="synComment">;; ハッシュのキーと組になる値を表示する</span>
<span class="synSpecial">(</span>hash-table-for-each <span class="synStatement">hash-table</span> <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>key value<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">print</span> #`<span class="synConstant">&#34;,|key| : ,|value|&#34;</span><span class="synSpecial">)))</span>
<span class="synComment">;; ハッシュのキーと値をベクターにする</span>
<span class="synSpecial">(</span>hash-table-map <span class="synStatement">hash-table</span> <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>key value<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">vector</span> key value<span class="synSpecial">)))</span>
</pre>
</div>