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

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