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

Last change on this file since 3995 was 3995, checked in by yura, 12 years ago
  • Rewritten.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1// $Id: List2.rf 3995 2008-10-24 10:21:05Z yura $
2
3$module "refal.plus.List2";
4
5$use Apply Access Arithm Compare;
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;
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 Paren_Term term = (term);
120Paren_Term term = (term);
121
122$func ConcatHelp t.term = e.expr;
123ConcatHelp {
124  (e.expr) = e.expr;
125  t.term   = t.term;
126};
127
128$public $func Concat e.lists = e.list;
129Concat e.lists = <Map &ConcatHelp e.lists>;
130
131//Nub e.expr = <NubBy &EqTerms e.expr>;
132//
133//NubBy s.eqTerms e.expr =
134//  e.expr () $iter {
135//    e.expr : term e.rest, {
136//      <Any s.eqTerms term (e.selected)> = e.rest (e.selected);
137//      e.rest (e.selected (term));
138//    };
139//  } :: e.expr (e.selected),
140//  e.expr : /*empty*/ =
141//  <Concat e.selected>;
142//
143//Replicate s.n e.expr =
144//  s.n /*e.list*/ $iter
145//    <Arithm.Sub s.n 1> e.list e.expr
146//  :: s.n e.list,
147//  <Le (s.n) (0)> =
148//  e.list;
149//
150//
151//
152
153$func QSort_Split t.num e.list = (e.list) (e.list);
154QSort_Split t.num e.list,
155  () () (e.list) $iter {
156    e.list : t.head e.tail, {
157      <Le (t.head) (t.num)>,
158        (e.smaller t.head) (e.greater) (e.tail);
159      (e.smaller) (e.greater t.head) (e.tail);
160    };
161  } :: (e.smaller) (e.greater) (e.list),
162  e.list : /*empty*/,
163  (e.smaller) (e.greater);
164
165$public $func QSort e.list = e.list;
166QSort {
167  /* empty */   = /* empty */;
168  t.head e.tail = <QSort_Split t.head e.tail> :: (e.smaller) (e.greater),
169    <QSort e.smaller> t.head <QSort e.greater>;
170};
171
172//SortBy s.cmpTerms e.list =
173//  <Length e.list> :: s.len,
174//  {
175//    <Le (s.len) (1)> = e.list;
176//    <Div s.len 2> :: s.k =
177//      <Merge s.cmpTerms (<SortBy s.cmpTerms <Left 0 s.k e.list>>) (<SortBy s.cmpTerms <Middle s.k 0 e.list>>)>;
178//  };
179//
180//$func Merge s.cmpTerms (e.left) (e.right) = e.merged_list;
181//Merge s.cmpTerms (e.left) (e.right) =
182//  (e.left) (e.right) $iter {
183//    e.left : t.l e.left_rest, e.right : t.r e.right_rest = {
184//      <Apply s.cmpTerms t.l t.r> : '<' = (e.left_rest) (e.right) e.merged t.l;
185//      = (e.left) (e.right_rest) e.merged t.r;
186//    };
187//  } :: (e.left) (e.right) e.merged,
188//  \{
189//    e.left  : /*empty*/ = e.merged e.right;
190//    e.right : /*empty*/ = e.merged e.left;
191//  };
192//
193//Sort e.list = <SortBy &CompareTerms e.list>;
Note: See TracBrowser for help on using the repository browser.