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

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