65 lines
6.9 KiB
Markdown
65 lines
6.9 KiB
Markdown
---
|
||
title: 自力でmaxを定義する – なにかうまくいかないや
|
||
author: kazu634
|
||
date: 2008-09-24
|
||
url: /2008/09/24/_1112/
|
||
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:4297;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
||
categories:
|
||
- gauche
|
||
- Lisp
|
||
|
||
---
|
||
<div class="section">
|
||
<p>
|
||
とりあえず作ってみた:
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>my_length lis<span class="synSpecial">)</span>
|
||
<span class="synSpecial">(</span><span class="synStatement">if</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">+</span> <span class="synConstant">1</span> <span class="synSpecial">(</span>my_length <span class="synSpecial">(</span><span class="synStatement">cdr</span> lis<span class="synSpecial">)))))</span>
|
||
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>my_max<span class="synStatement"> . </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> #f]
|
||
[<span class="synSpecial">(</span><span class="synStatement">=</span> <span class="synSpecial">(</span>my_length lis<span class="synSpecial">)</span> <span class="synConstant">1</span><span class="synSpecial">)</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>my_length lis<span class="synSpecial">)</span> <span class="synConstant">2</span><span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cond</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><span class="synStatement">cadr</span> lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">cadr</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><span class="synStatement">cadr</span> lis<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span>]<span class="synSpecial">)</span>]
|
||
[<span class="synSpecial">(</span><span class="synStatement">>=</span> <span class="synSpecial">(</span>my_length lis<span class="synSpecial">)</span> <span class="synConstant">3</span><span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cond</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><span class="synStatement">cadr</span> lis<span class="synSpecial">))</span> <span class="synSpecial">(</span>my_max <span class="synSpecial">(</span><span class="synStatement">cons</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cddr</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><span class="synStatement">cadr</span> lis<span class="synSpecial">))</span> <span class="synSpecial">(</span>my_max <span class="synSpecial">(</span><span class="synStatement">cons</span> <span class="synSpecial">(</span><span class="synStatement">car</span> lis<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cddr</span> lis<span class="synSpecial">)))</span>]<span class="synSpecial">)</span>]<span class="synSpecial">))</span>
|
||
</pre>
|
||
|
||
<p>
|
||
でもこれだと、
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
gosh> <span class="synSpecial">(</span>my_max <span class="synConstant">1</span> <span class="synConstant">2</span> <span class="synConstant">34</span> <span class="synConstant">5</span> <span class="synConstant">6</span><span class="synSpecial">)</span>
|
||
<span class="synSpecial">(</span><span class="synConstant">2</span> <span class="synConstant">34</span> <span class="synConstant">5</span> <span class="synConstant">6</span><span class="synSpecial">)</span>
|
||
</pre>
|
||
|
||
<p>
|
||
可変長引数の扱いについてもう一度見直してみようっと。可変長引数じゃなければ(=引数が「. lis」じゃなくて「lis」にして、リストを引数に渡してあげると)、希望通りの動作になる。たぶん可変長引数に特有のことなんだと思う。たぶんねー。
|
||
</p>
|
||
|
||
<blockquote>
|
||
<ul>
|
||
<li>
|
||
「gauche」に関連する最近のエントリ <ul>
|
||
<li>
|
||
<a href="http://d.hatena.ne.jp/sirocco634/20080923/1222179326" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080923/1222179326', ' 『プログラミングGauche』自分でmaxを定義してみる(アイディアを練ってみる) – 武蔵の日記');" target="_blank"> 『プログラミングGauche』自分でmaxを定義してみる(アイディアを練ってみる) – 武蔵の日記</a>
|
||
</li>
|
||
<li>
|
||
<a href="http://d.hatena.ne.jp/sirocco634/20080921/1221983678" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080921/1221983678', ' 車輪の再発名だけど、ユークリッドの互除法 – 武蔵の日記');" target="_blank"> 車輪の再発名だけど、ユークリッドの互除法 – 武蔵の日記</a>
|
||
</li>
|
||
<li>
|
||
<a href="http://d.hatena.ne.jp/sirocco634/20080915/4062138271" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080915/4062138271', '武蔵の日記');" target="_blank">武蔵の日記</a>
|
||
</li>
|
||
<li>
|
||
<a href="http://d.hatena.ne.jp/sirocco634/20080907/1220766982" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080907/1220766982', ' 練習問題を解いている – 武蔵の日記');" target="_blank"> 練習問題を解いている – 武蔵の日記</a>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</blockquote>
|
||
</div> |