blog/content/post/2008/09/06/2008-09-06-00001022.md

92 lines
5.4 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: ソフト開発の過去問で出てきた再帰をGaucheで書く
author: kazu634
date: 2008-09-06
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:4259;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- つれづれ
---
<div class="section">
<p>
ソフトウェア開発者試験ででてきた再帰を使った関数をGaucheで書いてみた。元の関数を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 Routine === */</span>
<span class="synComment">/* ==================== */</span>
<span class="synType">void</span> prnt_digit(<span class="synType">int</span> n)
{
<span class="synStatement">if</span> (<span class="synConstant"></span> == n){
<span class="synStatement">return</span>;
}
printf(<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, n);
prnt_digit(n - <span class="synConstant">1</span>);
printf(<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, n);
}
<span class="synType">int</span> main(<span class="synType">int</span> argc, <span class="synType">char</span> *argv[])
{
prnt_digit(<span class="synConstant">5</span>);
printf(<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synStatement">return</span> <span class="synConstant"></span>;
}
</pre>
<p>
これをgaucheで書いてみる:
</p>
<pre class="syntax-highlight">
<span class="synComment">;; lambdaを使わないバージョン</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>proc n<span class="synSpecial">)</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>tmp_proc temp<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">print</span> n<span class="synSpecial">)</span>
<span class="synSpecial">(</span>proc <span class="synSpecial">(</span><span class="synStatement">-</span> n <span class="synConstant">1</span><span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synStatement">print</span> 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"></span><span class="synSpecial">)</span>
n
<span class="synSpecial">(</span>tmp_proc <span class="synSpecial">(</span><span class="synStatement">-</span> n <span class="synConstant">1</span><span class="synSpecial">))))</span>
<span class="synComment">;; がんばってlambda使ってみたバージョン</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>proc_1 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"></span><span class="synSpecial">)</span>
n
<span class="synSpecial">((</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>temp<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">print</span> n<span class="synSpecial">)</span>
<span class="synSpecial">(</span>proc_1 <span class="synSpecial">(</span><span class="synStatement">-</span> n <span class="synConstant">1</span><span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synStatement">print</span> n<span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synStatement">-</span> n <span class="synConstant">1</span><span class="synSpecial">))))</span>
</pre>
<p>
lambdaは手続きを返すんだから「(」を二重に重ねないと引数をlambdaの無名関数に渡すことができないわけですね。なるほど。
</p>
<p>
追記:
</p>
<p>
condは暗黙でbeginが省略されていると解釈される。ifはそうじゃない。だから今回の場合は
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>proc_2 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"></span><span class="synSpecial">)</span>
n
<span class="synSpecial">(</span>begin
<span class="synSpecial">(</span><span class="synStatement">print</span> n<span class="synSpecial">)</span>
<span class="synSpecial">(</span>proc_1 <span class="synSpecial">(</span><span class="synStatement">-</span> n <span class="synConstant">1</span><span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synStatement">print</span> n<span class="synSpecial">))))</span>
</pre>
<p>
こうでもいいわけか。
</p>
</div>