blog/content/post/2010-09-05-00001378.md

57 lines
5.7 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: 'Lispの問題 from 「L-99: Ninety-Nine Lisp Problems」'
author: kazu634
date: 2010-09-05
url: /2010/09/05/_1562/
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:5339;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- gauche
- Lisp
---
<div class="section">
<p>
またまた頑張ってみました。letrecを使って、結果を渡しながらやっている。もしかしたら、継続で書き換えられる
</p>
<pre class="syntax-highlight">
<span class="synComment">;; P07 (**) Flatten a nested list structure.</span>
<span class="synComment">;; Transform a list, possibly holding lists as elements into a `flat' list by replacing each list with its elements (recursively).</span>
<span class="synComment">;; Example:</span>
<span class="synComment">;; * (my-flatten '(a (b (c d) e)))</span>
<span class="synComment">;; (A B C D E)</span>
<span class="synComment">;; Hint: Use the predefined functions list and append.</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span><span class="synStatement">atom</span>? l<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">and</span> <span class="synSpecial">(</span><span class="synStatement">not</span> <span class="synSpecial">(</span>pair? l<span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synStatement">not</span> <span class="synSpecial">(</span><span class="synStatement">null</span>? l<span class="synSpecial">))))</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>my-flatten l<span class="synSpecial">)</span>
<span class="synSpecial">(</span>letrec <span class="synSpecial">((</span>temp <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>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> result]
[<span class="synSpecial">(</span><span class="synStatement">atom</span>? <span class="synSpecial">(</span><span class="synStatement">car</span> l<span class="synSpecial">))</span> <span class="synSpecial">(</span>temp <span class="synSpecial">(</span><span class="synStatement">append</span> result
<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">cdr</span> l<span class="synSpecial">))</span>]
[else <span class="synSpecial">(</span>temp <span class="synSpecial">(</span>temp result <span class="synSpecial">(</span><span class="synStatement">car</span> l<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>temp <span class="synSpecial">'()</span> l<span class="synSpecial">)))</span>
<span class="synSpecial">(</span>my-flatten <span class="synSpecial">'(</span>a <span class="synSpecial">(</span>b <span class="synSpecial">(</span>c d<span class="synSpecial">)</span> e<span class="synSpecial">)))</span>
<span class="synComment">;; P08 (**) Eliminate consecutive duplicates of list elements.</span>
<span class="synComment">;; If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed.</span>
<span class="synComment">;; Example:</span>
<span class="synComment">;; * (compress '(a a a a b c c a a d e e e e))</span>
<span class="synComment">;; (A B C A D E)</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>compress l<span class="synSpecial">)</span>
<span class="synSpecial">(</span>letrec <span class="synSpecial">((</span>temp <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>front 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> result]
[<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>temp front
result
<span class="synSpecial">(</span><span class="synStatement">cdr</span> l<span class="synSpecial">))</span>]
[else <span class="synSpecial">(</span>temp <span class="synSpecial">(</span><span class="synStatement">car</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> <span class="synSpecial">(</span><span class="synStatement">car</span> l<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>temp <span class="synSpecial">'()</span> <span class="synSpecial">'()</span> l<span class="synSpecial">)))</span>
<span class="synSpecial">(</span>compress <span class="synSpecial">'(</span>a a a a b c c a a d e e e e<span class="synSpecial">))</span>
</pre>
</div>