blog/content/post/2008/08/20/2008-08-20-00001007.md

84 lines
6.4 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-20
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:4225;}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/hayamiz/" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/hayamiz/', 'id:hayamiz');">id:hayamiz</a>がEmacs勉強会でお勧めしてた<a href="http://www.sampou.org/scheme/t-y-scheme/" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.sampou.org/scheme/t-y-scheme/', 'Index of /scheme/t-y-scheme');" target="_blank">Index of /scheme/t-y-scheme</a>を読み始める。これをやってから、もういちど『<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>
letrecとか完全に誤解していた気がする
</h4>
<p>
わかったこと:
</p>
<p>
letはlambdaを言い換えている構文。だからブロックを作って、そのブロックの中で通用するローカル変数を持って結果を返しているイメージ…なのかなたぶん。lispとperlで比較:
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span><span class="synStatement">let</span> <span class="synSpecial">((</span>a <span class="synConstant">3</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span>b <span class="synConstant">5</span><span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synStatement">+</span> <span class="synSpecial">(</span><span class="synStatement">*</span> a a<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">*</span> b b<span class="synSpecial">)))</span>
<span class="synConstant">34</span>
</pre>
<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">$a</span> = <span class="synConstant">3</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$b</span> = <span class="synConstant">5</span>;
<span class="synStatement">print</span>((<span class="synIdentifier">$a</span> * <span class="synIdentifier">$a</span>) + (<span class="synIdentifier">$b</span> + <span class="synIdentifier">$b</span>), <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synComment"># print ($a); # これはエラーになる</span>
</pre>
<p>
letrecはlambdaで再帰的な手続きを作るときに用いるイメージなのかなうでも、perlだとforeachとかすごくしたくなるかも数字かどうかのチェックは省略
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>letrec <span class="synSpecial">((</span>sum <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>lis<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">cond</span> [<span class="synSpecial">(</span><span class="synStatement">null</span>? lis<span class="synSpecial">)</span> <span class="synConstant"></span>]
[<span class="synSpecial">(</span><span class="synStatement">number</span>? <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">+</span> <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span>sum <span class="synSpecial">(</span><span class="synStatement">cdr</span> lis<span class="synSpecial">)))</span>]
[else <span class="synSpecial">(</span>sum <span class="synSpecial">(</span><span class="synStatement">cdr</span> lis<span class="synSpecial">))</span>]<span class="synSpecial">))))</span>
<span class="synSpecial">(</span>sum <span class="synSpecial">'(</span>1 2 3 4 5<span class="synSpecial">)))</span>
<span class="synConstant">15</span>
</pre>
<pre class="syntax-highlight">
<span class="synComment"># === use ===</span>
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">my</span> $ res = <span class="synConstant"></span>;
<span class="synComment"># === main ===</span>
<span class="synStatement">my</span> <span class="synIdentifier">@i</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="synStatement">print</span> (<span class="synIdentifier">&#38;sub_sum</span>(<span class="synIdentifier">@i</span>), <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synComment"># === sub ===</span>
<span class="synStatement">sub</span><span class="synIdentifier"> sub_sum</span>{
<span class="synStatement">my</span> <span class="synIdentifier">$x</span>;
<span class="synStatement">my</span> <span class="synIdentifier">@y</span>;
(<span class="synIdentifier">$x</span>, <span class="synIdentifier">@y</span>) = <span class="synIdentifier">@_</span>;
<span class="synStatement">if</span> (<span class="synConstant"></span> == <span class="synIdentifier">@y</span>){
<span class="synStatement">return</span> <span class="synConstant"></span>;
} <span class="synStatement">else</span> {
<span class="synIdentifier">$res</span> = <span class="synIdentifier">$x</span> + <span class="synIdentifier">&#38;sub_sum</span>(<span class="synIdentifier">@y</span>);
}
<span class="synStatement">return</span> <span class="synIdentifier">$res</span>;
}
</pre>
</div>