45 lines
4.1 KiB
Markdown
45 lines
4.1 KiB
Markdown
---
|
||
title: Ninety-Nine Lisp Problems – P09 の解答
|
||
author: kazu634
|
||
date: 2010-11-22
|
||
url: /2010/11/22/_1628/
|
||
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:5387;}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://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html', 'L-99: Ninety-Nine Lisp Problems');" target="_blank">L-99: Ninety-Nine Lisp Problems</a>の問題を解答してみたよ。なんか、エレガントではないなぁ:
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synComment">;; P09 (**) Pack consecutive duplicates of list elements into sublists.</span>
|
||
<span class="synComment">;; If a list contains repeated elements they should be placed in separate sublists.</span>
|
||
<span class="synComment">;;</span>
|
||
<span class="synComment">;; Example:</span>
|
||
<span class="synComment">;; * (pack '(a a a a b c c a a d e e e e))</span>
|
||
<span class="synComment">;; ((A A A A) (B) (C C) (A A) (D) (E E E 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>pack <span class="synSpecial">'(</span>a a a a b c c a a d e e e e<span class="synSpecial">))</span>
|
||
</pre>
|
||
</div> |