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

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