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

Last change on this file since 4004 was 4004, 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.5 KB
Line 
1// $Id: List2.rf 4004 2008-10-28 12:56:29Z 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$public $func Split t.func e.list = (e.true_list) (e.false_list);
66Split t.func e.list =
67  () () e.list $iter {
68    e.list : t.head e.tail, {
69      <Apply t.func t.head> :: e = (e.true_list t.head) (e.false_list) e.tail;
70      = (e.true_list) (e.false_list t.head) e.tail;
71    };
72  } :: (e.true_list) (e.false_list) e.list, e.list : /*empty*/ =
73  (e.true_list) (e.false_list);
74
75$public $func? IsElem e.list t.item = ;
76IsElem e t.item e t.item;
77
78$public $func Foldl t.func (e.value) e.list = e.value;
79Foldl t.func (e.value) e.list =
80  (e.value) e.list $iter {
81    e.list : t.head e.tail = (<Apply t.func e.value t.head>) e.tail;
82  } :: (e.value) e.list, e.list : /*empty*/ =
83  e.value;
84
85$public $func Foldl1 t.func v.list = e.value;
86Foldl1 t.func t.head e.tail = <Foldr t.func (t.head) e.tail>;
87
88$public $func Foldr t.func (e.value) e.list = e.value;
89Foldr t.func (e.value) e.list =
90  (e.value) e.list $iter {
91    e.list : e.init t.last = (<Apply t.func t.last e.value>) e.init;
92  } :: (e.value) e.list, e.list : /*empty*/ =
93  e.value;
94
95$public $func Foldr1 t.func v.list = e.value;
96Foldr1 t.func e.init t.last = <Foldr t.func (t.last) e.init>;
97
98$public $func? All t.func e.list = ;
99All t.func e.list =
100  e.list $iter {
101    e.list : t.head e.tail = <Apply t.func t.head> :: e, e.tail;
102  } :: e.list, e.list : /*empty*/;
103
104$public $func? Any t.func e.list = ;
105Any 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
111//$const Concat = (&Map &Deparen_Term);
112//
113//<Const e.list> ~~~ <Apply &Concat e.list>;
114
115//Paren expr = <Map &Paren_Term (expr)>;
116//
117//Reverse {
118//  term e.list = <Reverse e.list> term;
119//  /*empty*/ = /*empty*/;
120//};
121
122$public $func Intersperse (e.sep) e.list = e.list;
123Intersperse {
124  (e.sep) /*empty*/ = /*empty*/;
125  (e.sep) t.head0 = t.head0;
126  (e.sep) t.head0 e.tail0 =
127    (t.head0 e.sep) e.tail0 $iter {
128      e.list : t.head e.tail = (e.new_list t.head e.sep) e.tail;
129    } :: (e.new_list) e.list, e.list : t.last =
130    e.new_list t.last;
131};
132
133$public $func Separate (e.sep) e.list = e.listOfLists;
134Separate (e.sep) e.list =
135  () e.list $iter {
136    e.list : e.before e.sep e.after = (e.ll (e.before)) e.after;
137    = (e.ll (e.list));
138  } :: (e.ll) e.list, e.list : /*empty*/ =
139  e.ll;
140
141$func ConcatHelp t.term = e.expr;
142ConcatHelp {
143  (e.expr) = e.expr;
144  t.term   = t.term;
145};
146
147$public $func Id e.expr = e.expr;
148Id e.expr = e.expr;
149
150$public $func Concat e.lists = e.list;
151Concat e.lists = <Map &ConcatHelp e.lists>;
152
153$public $func? EqTerms t.term t.term = ;
154EqTerms t1 t1;
155
156$public $func Nub e.list = e.list;
157Nub e.list = <NubBy &EqTerms e.list>;
158
159$public $func NubBy t.eqTerms e.list = e.list;
160NubBy t.eqTerms e.list =
161  () e.list $iter {
162    e.list : t.head e.tail, {
163      <Any (t.eqTerms t.head) e.new_list> = (e.new_list) e.tail;
164      = (e.new_list t.head) e.tail;
165    };
166  } :: (e.new_list) e.list, e.list : /*empty*/ =
167  e.new_list;
168
169$public $func Frequencies e.list = e.list;
170Frequencies e.list =
171  <Table> :: s.table,
172  {
173    e.list : e t.term e,
174      { <Lookup s.table t.term>; 0; } : s.num,
175      <Bind s.table (t.term) (<Add s.num 1>)>,
176      $fail;
177    <MapIn &Concat <Entries s.table>>;
178  };
179
180$public $func SwapR e.list = e.list;
181SwapR {
182  e.init t.last = t.last e.init;
183  e.list = e.list;
184};
185
186//Replicate s.n e.expr =
187//  s.n /*e.list*/ $iter
188//    <Arithm.Sub s.n 1> e.list e.expr
189//  :: s.n e.list,
190//  <Le (s.n) (0)> =
191//  e.list;
192
193$func QSort_Split t.num e.list = (e.list) (e.list);
194QSort_Split t.num e.list,
195  () () e.list $iter {
196    e.list : t.head e.tail, {
197      <Le (t.head) (t.num)> = (e.smaller t.head) (e.greater) e.tail;
198      = (e.smaller) (e.greater t.head) e.tail;
199    };
200  } :: (e.smaller) (e.greater) e.list, e.list : /*empty*/ =
201  (e.smaller) (e.greater);
202
203$public $func QSort e.list = e.list;
204QSort {
205  /* empty */   = /* empty */;
206  t.head e.tail = <QSort_Split t.head e.tail> :: (e.smaller) (e.greater),
207    <QSort e.smaller> t.head <QSort e.greater>;
208};
209
210$public $func CompareTerms t.term t.term = s.cmp;
211CompareTerms t.term1 t.term2 = <Compare (t.term1) (t.term2)>;
212
213$public $func Sort e.list = e.list;
214Sort e.list = <SortBy &CompareTerms e.list>;
215
216$public $func SortBy t.cmpTerms e.list = e.list;
217SortBy t.cmpTerms e.list =
218  <Div <Length e.list> 2> : {
219    0 = e.list;
220    s.k = <Merge t.cmpTerms (<SortBy t.cmpTerms <Left 0 s.k e.list>>) (<SortBy t.cmpTerms <Middle s.k 0 e.list>>)>;
221  };
222
223$func Merge t.cmpTerms (e.left) (e.right) = e.merged_list;
224Merge t.cmpTerms (e.left) (e.right) =
225  (e.left) (e.right) $iter {
226    e.left : t.l e.left_rest, e.right : t.r e.right_rest = {
227     <Apply t.cmpTerms t.l t.r> : '<' = e.merged t.l (e.left_rest) (e.right);
228      = e.merged t.r (e.left) (e.right_rest);
229    };
230  } :: e.merged (e.left) (e.right),
231  \{
232    e.left  : /*empty*/ = e.merged e.right;
233    e.right : /*empty*/ = e.merged e.left;
234  };
Note: See TracBrowser for help on using the repository browser.