source: to-imperative/trunk/java/refal/refal/plus/List2.rf @ 4000

Last change on this file since 4000 was 4000, checked in by yura, 12 years ago
  • Frequencies, SwapR
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1// $Id: List2.rf 4000 2008-10-25 17:04:25Z yura $
2
3$module "refal.plus.List2";
4
5$use Apply Access Arithm Compare Table;
6
7$func SimplifyFunction t.func e.arg = s.func e.arg;
8SimplifyFunction t.func e.arg =
9  t.func e.arg $iter {
10    t.func : (t.func1 e.arg1) = t.func1 e.arg1 e.arg;
11  } :: t.func e.arg, t.func : s.funcRef =
12  s.funcRef e.arg;
13
14$public $func? Apply t.func e.arg = e.res;
15Apply t.func e.arg = <Apply.Apply <SimplifyFunction t.func e.arg>>;
16
17$public $func PApply t.func e.arg = t.func;
18PApply t.func e.arg = <SimplifyFunction t.func e.arg> :: s.func e.arg,
19  {
20    e.arg : /* empty */ = s.func;
21    = (s.func e.arg);
22  };
23
24$public $func Map t.func e.list = e.list;
25Map t.func e.list =
26  () e.list $iter {
27    e.list : t.head e.tail = (e.new_list <Apply t.func t.head>) e.tail;
28  } :: (e.new_list) e.list, e.list : /*empty*/ =
29  e.new_list;
30
31$func MapInHelp t.func t.term = t.term;
32MapInHelp t.func (e.expr) = (<Apply t.func e.expr>);
33
34$public $func MapIn t.func e.list = e.list;
35MapIn t.func e.list = <Map (&MapInHelp t.func) e.list>;
36
37$func FilterHelp t.func t.term = e.term_or_empty;
38FilterHelp t.func t.term = { <Apply t.func t.term> :: e = t.term; ; };
39
40$public $func Filter t.func e.list = e.list;
41Filter t.func e.list = <Map (&FilterHelp t.func) e.list>;
42
43$public $func Split t.func e.list = (e.true_list) (e.false_list);
44Split t.func e.list =
45  () () e.list $iter {
46    e.list : t.head e.tail, {
47      <Apply t.func t.head> :: e = (e.true_list t.head) (e.false_list) e.tail;
48      = (e.true_list) (e.false_list t.head) e.tail;
49    };
50  } :: (e.true_list) (e.false_list) e.list, e.list : /*empty*/ =
51  (e.true_list) (e.false_list);
52
53$public $func? IsElem e.list t.item = ;
54IsElem e t.item e t.item;
55
56$public $func Foldl t.func (e.value) e.list = e.value;
57Foldl t.func (e.value) e.list =
58  (e.value) e.list $iter {
59    e.list : t.head e.tail = (<Apply t.func e.value t.head>) e.tail;
60  } :: (e.value) e.list, e.list : /*empty*/ =
61  e.value;
62
63$public $func Foldl1 t.func v.list = e.value;
64Foldl1 t.func t.head e.tail = <Foldr t.func (t.head) e.tail>;
65
66$public $func Foldr t.func (e.value) e.list = e.value;
67Foldr t.func (e.value) e.list =
68  (e.value) e.list $iter {
69    e.list : e.init t.last = (<Apply t.func t.last e.value>) e.init;
70  } :: (e.value) e.list, e.list : /*empty*/ =
71  e.value;
72
73$public $func Foldr1 t.func v.list = e.value;
74Foldr1 t.func e.init t.last = <Foldr t.func (t.last) e.init>;
75
76$public $func? All t.func e.list = ;
77All t.func e.list =
78  e.list $iter {
79    e.list : t.head e.tail = <Apply t.func t.head> :: e, e.tail;
80  } :: e.list, e.list : /*empty*/;
81
82$public $func? Any t.func e.list = ;
83Any t.func e.list = # \{
84  e.list $iter {
85    e.list : t.head e.tail = # \{ <Apply t.func t.head > :: e; }, e.tail;
86  } :: e.list, e.list : /*empty*/;
87};
88
89//$const Concat = (&Map &Deparen_Term);
90//
91//<Const e.list> ~~~ <Apply &Concat e.list>;
92
93//Paren expr = <Map &Paren_Term (expr)>;
94//
95//Reverse {
96//  term e.list = <Reverse e.list> term;
97//  /*empty*/ = /*empty*/;
98//};
99
100$public $func Intersperse (e.sep) e.list = e.list;
101Intersperse {
102  (e.sep) /*empty*/ = /*empty*/;
103  (e.sep) t.head0 = t.head0;
104  (e.sep) t.head0 e.tail0 =
105    (t.head0 e.sep) e.tail0 $iter {
106      e.list : t.head e.tail = (e.new_list t.head e.sep) e.tail;
107    } :: (e.new_list) e.list, e.list : t.last =
108    e.new_list t.last;
109};
110
111$public $func Separate (e.sep) e.list = e.listOfLists;
112Separate (e.sep) e.list =
113  () e.list $iter {
114    e.list : e.before e.sep e.after = (e.ll (e.before)) e.after;
115    = (e.ll (e.list));
116  } :: (e.ll) e.list, e.list : /*empty*/ =
117  e.ll;
118
119$func ConcatHelp t.term = e.expr;
120ConcatHelp {
121  (e.expr) = e.expr;
122  t.term   = t.term;
123};
124
125$public $func Id e.expr = e.expr;
126Id e.expr = e.expr;
127
128$public $func Concat e.lists = e.list;
129Concat e.lists = <Map &ConcatHelp e.lists>;
130
131$public $func? EqTerms t.term t.term = ;
132EqTerms t1 t1;
133
134$public $func Nub e.list = e.list;
135Nub e.list = <NubBy &EqTerms e.list>;
136
137$public $func NubBy t.eqTerms e.list = e.list;
138NubBy t.eqTerms e.list =
139  () e.list $iter {
140    e.list : t.head e.tail, {
141      <Any (t.eqTerms t.head) e.new_list> = (e.new_list) e.tail;
142      = (e.new_list t.head) e.tail;
143    };
144  } :: (e.new_list) e.list, e.list : /*empty*/ =
145  e.new_list;
146
147$public $func Frequencies e.list = e.list;
148Frequencies e.list =
149  <Table> :: s.table,
150  {
151    e.list : e t.term e,
152      { <Lookup s.table t.term>; 0; } : s.num,
153      <Bind s.table (t.term) (<Add s.num 1>)>,
154      $fail;
155    <MapIn &Concat <Entries s.table>>;
156  };
157
158$public $func SwapR e.list = e.list;
159SwapR {
160  e.init t.last = t.last e.init;
161  e.list = e.list;
162};
163
164//Replicate s.n e.expr =
165//  s.n /*e.list*/ $iter
166//    <Arithm.Sub s.n 1> e.list e.expr
167//  :: s.n e.list,
168//  <Le (s.n) (0)> =
169//  e.list;
170
171$func QSort_Split t.num e.list = (e.list) (e.list);
172QSort_Split t.num e.list,
173  () () e.list $iter {
174    e.list : t.head e.tail, {
175      <Le (t.head) (t.num)> = (e.smaller t.head) (e.greater) e.tail;
176      = (e.smaller) (e.greater t.head) e.tail;
177    };
178  } :: (e.smaller) (e.greater) e.list, e.list : /*empty*/ =
179  (e.smaller) (e.greater);
180
181$public $func QSort e.list = e.list;
182QSort {
183  /* empty */   = /* empty */;
184  t.head e.tail = <QSort_Split t.head e.tail> :: (e.smaller) (e.greater),
185    <QSort e.smaller> t.head <QSort e.greater>;
186};
187
188$public $func CompareTerms t.term t.term = s.cmp;
189CompareTerms t.term1 t.term2 = <Compare (t.term1) (t.term2)>;
190
191$public $func Sort e.list = e.list;
192Sort e.list = <SortBy &CompareTerms e.list>;
193
194$public $func SortBy t.cmpTerms e.list = e.list;
195SortBy t.cmpTerms e.list =
196  <Div <Length e.list> 2> : {
197    0 = e.list;
198    s.k = <Merge t.cmpTerms (<SortBy t.cmpTerms <Left 0 s.k e.list>>) (<SortBy t.cmpTerms <Middle s.k 0 e.list>>)>;
199  };
200
201$func Merge t.cmpTerms (e.left) (e.right) = e.merged_list;
202Merge t.cmpTerms (e.left) (e.right) =
203  (e.left) (e.right) $iter {
204    e.left : t.l e.left_rest, e.right : t.r e.right_rest = {
205     <Apply t.cmpTerms t.l t.r> : '<' = e.merged t.l (e.left_rest) (e.right);
206      = e.merged t.r (e.left) (e.right_rest);
207    };
208  } :: e.merged (e.left) (e.right),
209  \{
210    e.left  : /*empty*/ = e.merged e.right;
211    e.right : /*empty*/ = e.merged e.left;
212  };
Note: See TracBrowser for help on using the repository browser.