source: to-imperative/trunk/java/refal/refal/plus/List.rf @ 3752

Last change on this file since 3752 was 3752, checked in by yura, 13 years ago
  • Variables name in functions format are more accurate.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1// $Id: List.rf 3752 2008-04-20 12:09:20Z yura $
2
3$use Apply Access Arithm Compare;
4
5$func Deparen_Term term = expr;
6Deparen_Term {
7  (expr) = expr;
8  term   = term;
9};
10
11$func Paren_Term term = (term);
12Paren_Term term = (term);
13
14Id e1 = e1;
15
16Append (e1) e2 = e2 e1;
17
18Zip (e.list1) (e.list2) =
19  (e.list1) (e.list2) /*empty*/ $iter {
20    e.list1 : t.first1 e.rest1,
21      e.list2 : t.first2 e.rest2 =
22      (e.rest1) (e.rest2) e.new_list (t.first1 t.first2);
23  } :: (e.list1) (e.list2) e.new_list,
24  e.list1 : /*empty*/ =
25  e.new_list;
26
27Map s.Fname e.Fargs (e.list) =
28  () e.list $iter {
29    e.list : t.item e.rest =
30      (e.new_list <Apply s.Fname e.Fargs t.item>) e.rest;
31  } :: (e.new_list) e.list,
32  e.list : /*empty*/ =
33  e.new_list;
34
35MapIn s.Fname e.Fargs (e.list) =
36  () e.list $iter {
37    e.list : (e.item) e.rest =
38      (e.new_list (<Apply s.Fname e.Fargs e.item>)) e.rest;
39  } :: (e.new_list) e.list,
40  e.list : /*empty*/ =
41  e.new_list;
42
43Filter s.Fname e.Fargs (e.list) =
44  () e.list $iter {
45    e.list : t.item e.rest, {
46      <Apply s.Fname e.Fargs t.item> : e =
47        (e.new_list t.item) e.rest;
48      (e.new_list) e.rest;
49    };
50  } :: (e.new_list) e.list,
51  e.list : /*empty*/ =
52  e.new_list;
53
54Split s.Fname e.Fargs (e.list) =
55  () () e.list $iter {
56    e.list : t.item e.rest, {
57      <Apply s.Fname e.Fargs t.item> : e =
58        (e.true_list t.item) (e.false_list) e.rest;
59      (e.true_list) (e.false_list t.item) e.rest;
60    };
61  } :: (e.true_list) (e.false_list) e.list,
62  e.list : /*empty*/ =
63  (e.true_list) (e.false_list);
64
65IsElem e.list t.item =
66  e.list : e t.item e;
67
68Foldr s.Fname e.Fargs (e.value) (e.list) =
69  (e.value) e.list $iter {
70    e.list : e.something t.last =
71      (<Apply s.Fname e.Fargs t.last e.value>) e.something;
72  } :: (e.value) e.list,
73  e.list : /*empty*/ =
74  e.value;
75
76Foldr1 s.Fname e.Fargs (e.list), {
77  e.list : e.something t.last =
78    <Foldr s.Fname e.Fargs (t.last) (e.something)>;
79  /*empty*/;
80};
81
82All s.Fname e.Fargs (e.list) =
83  e.list $iter {
84    e.list : t.item e.rest =
85      <Apply s.Fname e.Fargs t.item> : e,
86      e.rest;
87  } :: e.list,
88  e.list : /*empty*/;
89
90Any s.Fname e.Fargs (e.list) = # \{
91  e.list $iter {
92    e.list : t.item e.rest =
93      # \{ <Apply s.Fname e.Fargs t.item> : e; },
94      e.rest;
95  } :: e.list,
96  e.list : /*empty*/;
97};
98
99Concat e.lists = <Map &Deparen_Term (e.lists)>;
100
101Paren expr = <Map &Paren_Term (expr)>;
102
103Reverse {
104  term e.list = <Reverse e.list> term;
105  /*empty*/ = /*empty*/;
106};
107
108Intersperse {
109  (e.sep) /*empty*/ = /*empty*/;
110  (e.sep) t1 = t1;
111  (e.sep) t1 e.list =
112    (t1 e.sep) e.list $iter {
113      e.list : t2 e.rest =
114        (e.new_list t2 e.sep) e.rest;
115    } :: (e.new_list) e.list,
116    e.list : t2 =
117    e.new_list t2;
118};
119
120Separate (e.sep) e.list =
121  e.list (/*e.ll*/) $iter {
122    e.list : e1 e.sep e2 = e2 (e.ll (e1));
123    (e.ll (e.list));
124  } :: e.list (e.ll),
125  e.list : /*empty*/ =
126  e.ll;
127
128Nub e.expr =
129  e.expr () $iter {
130    e.expr : term e.rest, {
131      e.selected : e term e = e.rest (e.selected);
132      e.rest (e.selected term);
133    };
134  } :: e.expr (e.selected),
135  e.expr : /*empty*/ =
136  e.selected;
137
138Replicate s.n e.expr =
139  s.n /*e.list*/ $iter
140    <Arithm.Sub s.n 1> e.list e.expr
141  :: s.n e.list,
142  <Le (s.n) (0)> =
143  e.list;
144
145/*
146 * Returns number of e.expr entries in e.source and e.source without all e.expr's.
147 */
148//Entries e.expr (e.source) =
149//  0 e.source () $iter {
150//    e.source : e1 e.expr e2 = <"+" s.num 1> e2 (e.res e1);
151//    s.num (e.res e.source);
152//  } :: s.num e.source (e.res),
153//  e.source : /*empty*/ =
154//  s.num e.res;
155
156/*
157 * Add to the first list all new elements from the second one.
158 */
159Or (e.list1) e.list2 =
160  (e.list1) e.list2 $iter {
161    e.list2 : term e.rest,
162      {
163        e.list1 : e term e = (e.list1) e.rest;
164        (e.list1 term) e.rest;
165      };
166  } :: (e.list1) e.list2,
167  e.list2 : /*empty*/ =
168  e.list1;
169
170/*
171 * Choose from the first list all elements present in the second one.
172 */
173And (e.list1) e.list2 =
174  (e.list1) /*empty*/ $iter {
175    e.list1 : term e.rest,
176      {
177        e.list2 : e term e = (e.rest) e.and term;
178        (e.rest) e.and;
179      };
180  } :: (e.list1) e.and,
181  e.list1 : /*empty*/ =
182  e.and;
183
184/*
185 * Choose from the first list all elements not present in the second one.
186 */
187Sub (e.list1) e.list2 =
188  (e.list1) /*empty*/ $iter {
189    e.list1 : term e.rest,
190      {
191        e.list2 : e term e = (e.rest) e.not;
192        (e.rest) e.not term;
193      };
194  } :: (e.list1) e.not,
195  e.list1 : /*empty*/ =
196  e.not;
197
Note: See TracBrowser for help on using the repository browser.