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

Last change on this file since 3996 was 3996, 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: 5.6 KB
Line 
1// $Id: List2.rf 3996 2008-10-24 10:46:34Z 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 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 Concat e.lists = e.list;
126Concat e.lists = <Map &ConcatHelp e.lists>;
127
128$public $func? EqTerms t.term t.term = ;
129EqTerms t1 t1;
130
131$public $func Nub e.list = e.list;
132Nub e.list = <NubBy &EqTerms e.list>;
133
134$public $func NubBy t.eqTerms e.list = e.list;
135NubBy t.eqTerms e.list =
136  () e.list $iter {
137    e.list : t.head e.tail, {
138      <Any (t.eqTerms t.head) e.new_list> = (e.new_list) e.tail;
139      = (e.new_list (t.head)) e.tail;
140    };
141  } :: (e.new_list) e.list, e.list : /*empty*/ =
142  e.new_list;
143
144//Replicate s.n e.expr =
145//  s.n /*e.list*/ $iter
146//    <Arithm.Sub s.n 1> e.list e.expr
147//  :: s.n e.list,
148//  <Le (s.n) (0)> =
149//  e.list;
150
151$func QSort_Split t.num e.list = (e.list) (e.list);
152QSort_Split t.num e.list,
153  () () e.list $iter {
154    e.list : t.head e.tail, {
155      <Le (t.head) (t.num)> = (e.smaller t.head) (e.greater) e.tail;
156      = (e.smaller) (e.greater t.head) e.tail;
157    };
158  } :: (e.smaller) (e.greater) e.list, e.list : /*empty*/ =
159  (e.smaller) (e.greater);
160
161$public $func QSort e.list = e.list;
162QSort {
163  /* empty */   = /* empty */;
164  t.head e.tail = <QSort_Split t.head e.tail> :: (e.smaller) (e.greater),
165    <QSort e.smaller> t.head <QSort e.greater>;
166};
167
168$public $func CompareTerms t.term t.term = s.cmp;
169CompareTerms t.term1 t.term2 = <Compare (t.term1) (t.term2)>;
170
171$public $func Sort e.list = e.list;
172Sort e.list = <SortBy &CompareTerms e.list>;
173
174$public $func SortBy t.cmpTerms e.list = e.list;
175SortBy t.cmpTerms e.list =
176  <Length e.list> :: s.len,
177  {
178    <Le (s.len) (1)> = e.list;
179    <Div s.len 2> :: s.k =
180      <Merge t.cmpTerms (<SortBy t.cmpTerms <Left 0 s.k e.list>>) (<SortBy t.cmpTerms <Middle s.k 0 e.list>>)>;
181  };
182
183$func Merge t.cmpTerms (e.left) (e.right) = e.merged_list;
184Merge t.cmpTerms (e.left) (e.right) =
185  (e.left) (e.right) $iter {
186    e.left : t.l e.left_rest, e.right : t.r e.right_rest = {
187      <Apply t.cmpTerms t.l t.r> : '<' = e.merged t.l (e.left_rest) (e.right);
188      = e.merged t.r (e.left) (e.right_rest);
189    };
190  } :: e.merged (e.left) (e.right),
191  \{
192    e.left  : /*empty*/ = e.merged e.right;
193    e.right : /*empty*/ = e.merged e.left;
194  };
Note: See TracBrowser for help on using the repository browser.