blog/content/post/2010-05-11-00001360.md

48 lines
4.4 KiB
Markdown

---
title: Lispでシュワルツ変換
author: kazu634
date: 2010-05-11
url: /2010/05/11/_1524/
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:5251;}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/higepon/" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/higepon/', 'id:higepon');">id:higepon</a>さん主催のコードバトンで学んだシュワルツ変換。いま自分用のスクリプト作りのためにもう一度使っている。こんな感じ:
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>list-sort-by key-fn cmp xs<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">map</span> <span class="synStatement">cdr</span>
<span class="synSpecial">(</span><span class="synStatement">sort</span>
<span class="synSpecial">(</span><span class="synStatement">map</span>
<span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>x<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cons</span> <span class="synSpecial">(</span>key-fn x<span class="synSpecial">)</span> x<span class="synSpecial">))</span>
xs<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>a b<span class="synSpecial">)</span> <span class="synSpecial">(</span>cmp <span class="synSpecial">(</span><span class="synStatement">car</span> a<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">car</span> b<span class="synSpecial">))))))</span>
</pre>
<p>
こういうの考える人って頭いいなぁと思う。実行例はこんな感じになる:
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>define data <span class="synSpecial">'((</span>foo 3<span class="synSpecial">)</span> <span class="synSpecial">(</span>bar 1<span class="synSpecial">)</span> <span class="synSpecial">(</span>osaka 2<span class="synSpecial">)</span> <span class="synSpecial">(</span>okayama 0<span class="synSpecial">)))</span>
data
gosh&#62; <span class="synSpecial">(</span>list-sort-by <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>x<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> x<span class="synSpecial">))</span> <span class="synStatement">&#62;</span> data<span class="synSpecial">)</span>
<span class="synSpecial">((</span>foo <span class="synConstant">3</span><span class="synSpecial">)</span> <span class="synSpecial">(</span>osaka <span class="synConstant">2</span><span class="synSpecial">)</span> <span class="synSpecial">(</span>bar <span class="synConstant">1</span><span class="synSpecial">)</span> <span class="synSpecial">(</span>okayama <span class="synConstant"></span><span class="synSpecial">))</span>
gosh&#62; <span class="synSpecial">(</span>list-sort-by <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>x<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">cadr</span> x<span class="synSpecial">))</span> <span class="synStatement">&#60;</span> data<span class="synSpecial">)</span>
<span class="synSpecial">((</span>okayama <span class="synConstant"></span><span class="synSpecial">)</span> <span class="synSpecial">(</span>bar <span class="synConstant">1</span><span class="synSpecial">)</span> <span class="synSpecial">(</span>osaka <span class="synConstant">2</span><span class="synSpecial">)</span> <span class="synSpecial">(</span>foo <span class="synConstant">3</span><span class="synSpecial">))</span>
</pre>
<p>
上の例だと、リスト内の数字でソートしている。lambdaの中をごにょごにょすれば、任意の場所の値でソートすることが可能。
</p>
<p>
次はこれを元にして、sortじゃなくてfilterするようにしてみよう。
</p>
</div>