最近の更新 (Recent Changes)

2014-01-01
2013-01-04
2012-12-22
2012-12-15
2012-12-09

Wikiガイド(Guide)

サイドバー (Side Bar)


← 前のページに戻る

0.8 リストの平坦化

リストの平坦化とは、いくつかの階層で構成されたリストを単一の階層のリストに変換することです。

例えば、(a (b (c d) e (f (g)) h i) j k)というリストを、(a b c d e f g h i j k)のように変換します。


// リストの平坦化

// サブルーチン : リスト追加の定義

#xは()と#xのリスト追加です。

#zは#xと#yのリスト追加のとき、
(#a:#z)が(#a:#x)と#yのリスト追加です。


// メイン : リスト平坦化の定義

()は()のリスト平坦化です。

#xがリストであり、
#list1が#xのリスト平坦化であり、
#list2が#lのリスト平坦化であり、
#listが、#list1と#list2のリスト追加であるとき、
#listは、(#x:#l)のリスト平坦化です。

#list2が#lのリスト平坦化であり、
#listが、(#x)と#list2のリスト追加であるとき、
#listは、(#x:#l)のリスト平坦化です。

// 実行

#listは、 (a (b (c d) e (f (g)) h i) j k)のリスト平坦化ですか。

上記のプログラムをsamples/flatten.mrsにあるとして実行してみましょう。


$ descartes murasaki samples/flatten.mrs

#listは、 (a (b (c d) e (f (g)) h i) j k)のリスト平坦化ですか。
(a b c d e f g h i j k)は、(a (b (c d) e (f (g)) h i) j k)のリスト平坦化です。

このプログラムは次に示すようなデカルト言語のプログラムにコンパイルされて実行されたものです。


 <M2 リスト追加 #x (() #x)>
        ;
 <M2 リスト追加 (#a:#z) ((#a:#x) #y)> 
     <M2 リスト追加 #z (#x #y)>
        ;
 <M2 リスト平坦化 () (())>
        ;
 <M2 リスト平坦化 #list ((#x:#l))> 
     <M4 リスト #x ()> <M2 リスト平坦化 #list1 (#x)> 
     <M2 リスト平坦化 #list2 (#l)> <M2 リスト追加 #list (#list1 #list2)>
        ;
 <M2 リスト平坦化 #list ((#x:#l))> 
     <M2 リスト平坦化 #list2 (#l)> 
     <M2 リスト追加 #list ((#x) #list2)>
        ;
? <printf '#listは、 (a (b (c d) e (f (g)) h i) j k)のリスト平坦化ですか。' <\_n
>>;
? <回答 (<M2 リスト平坦化 #list ((a (b (c d) e (f (g)) h i) j k))>)>;
? <print>;


--

--