blog/content/post/2010/11/24/2010-11-24-00001430.md

73 lines
5.5 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: Ninety-nine Lisp Problems P10の解答
author: kazu634
date: 2010-11-24
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:5389;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- gauche
- Lisp
---
<div class="section">
<p>
P9の解答で作成した関数を元にして、リスト空リストへの変換を行う形で解答を作成してみました。つまり
</p>
<ol>
<li>
packでリストを作成する
</li>
<li>
packで作られたリストの中のリストを変換する手続きencode-listを作成する
</li>
<li>
packに対してencode-listをmapする
</li>
</ol>
<p>
感じでやってみました。
</p>
<h4>
ソース
</h4>
<pre class="syntax-highlight">
<span class="synComment">;; P10 (*) Run-length encoding of a list.</span>
<span class="synComment">;; Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as lists (N E) where N is the number of duplicates of the element E.</span>
<span class="synComment">;; Example:</span>
<span class="synComment">;; * (encode '(a a a a b c c a a d e e e e))</span>
<span class="synComment">;; ((4 A) (1 B) (2 C) (2 A) (1 D)(4 E))</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>pack-seed front sublist result l<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">cond</span>
[<span class="synSpecial">(</span><span class="synStatement">null</span>? l<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">append</span> result <span class="synSpecial">(</span><span class="synStatement">list</span> sublist<span class="synSpecial">))</span>]
[<span class="synSpecial">(</span><span class="synStatement">eq</span>? front <span class="synSpecial">(</span><span class="synStatement">car</span> l<span class="synSpecial">))</span> <span class="synSpecial">(</span>pack-seed front
<span class="synSpecial">(</span><span class="synStatement">append</span> sublist
<span class="synSpecial">(</span><span class="synStatement">list</span> front<span class="synSpecial">))</span>
result
<span class="synSpecial">(</span><span class="synStatement">cdr</span> l<span class="synSpecial">))</span>]
[else <span class="synSpecial">(</span>pack-seed <span class="synSpecial">(</span><span class="synStatement">car</span> l<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">list</span> <span class="synSpecial">(</span><span class="synStatement">car</span> l<span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">null</span>? result<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">null</span>? sublist<span class="synSpecial">)</span>
<span class="synSpecial">'()</span>
<span class="synSpecial">(</span><span class="synStatement">append</span> result <span class="synSpecial">(</span><span class="synStatement">list</span> sublist<span class="synSpecial">)))</span>
<span class="synSpecial">(</span><span class="synStatement">append</span> result <span class="synSpecial">(</span><span class="synStatement">list</span> sublist<span class="synSpecial">)))</span>
<span class="synSpecial">(</span><span class="synStatement">cdr</span> l<span class="synSpecial">))</span>]<span class="synSpecial">))</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>pack l<span class="synSpecial">)</span>
<span class="synSpecial">(</span>pack-seed #f <span class="synSpecial">'()</span> <span class="synSpecial">'()</span> l<span class="synSpecial">))</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>encode-list l<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">let</span> <span class="synSpecial">((</span>num <span class="synSpecial">(</span><span class="synStatement">length</span> l<span class="synSpecial">)))</span>
<span class="synSpecial">(</span><span class="synStatement">append</span> <span class="synSpecial">(</span><span class="synStatement">list</span> num<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">list</span> <span class="synSpecial">(</span><span class="synStatement">car</span> l<span class="synSpecial">)))))</span>
<span class="synSpecial">(</span>encode-list <span class="synSpecial">'(</span>a a a a a<span class="synSpecial">))</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>encode l<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">map</span> <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>l<span class="synSpecial">)</span>
<span class="synSpecial">(</span>encode-list l<span class="synSpecial">))</span>
<span class="synSpecial">(</span>pack l<span class="synSpecial">)))</span>
<span class="synSpecial">(</span>encode <span class="synSpecial">'(</span>a a a a b c c a a d e e e e<span class="synSpecial">))</span>
</pre>
</div>