source: to-imperative/trunk/library/List/refal/List.rf @ 2037

Last change on this file since 2037 was 2037, checked in by orlov, 15 years ago
  • Added List.Replicate function.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1// $Source$
2// $Revision: 2037 $
3// $Date: 2006-07-27 12:45:55 +0000 (Thu, 27 Jul 2006) $
4
5$use Apply Access Arithm Compare;
6
7$func Deparen-Term term = expr;
8Deparen-Term {
9  (expr) = expr;
10  term   = term;
11};
12
13$func Paren-Term term = (term);
14Paren-Term term = (term);
15
16Zip (e.list1) (e.list2) =
17  (e.list1) (e.list2) /*empty*/ $iter {
18    e.list1 : t.first1 e.rest1,
19      e.list2 : t.first2 e.rest2 =
20      (e.rest1) (e.rest2) e.new-list (t.first1 t.first2);
21  } :: (e.list1) (e.list2) e.new-list,
22  e.list1 : /*empty*/ =
23  e.new-list;
24
25Map s.Fname e.Fargs (e.list) =
26  () e.list $iter {
27    e.list : t.item e.rest =
28      (e.new-list <Apply s.Fname e.Fargs t.item>) e.rest;
29  } :: (e.new-list) e.list,
30  e.list : /*empty*/ =
31  e.new-list;
32
33Filter s.Fname e.Fargs (e.list) =
34  () e.list $iter {
35    e.list : t.item e.rest, {
36      <Apply s.Fname e.Fargs t.item> : e =
37        (e.new-list t.item) e.rest;
38      (e.new-list) e.rest;
39    };
40  } :: (e.new-list) e.list,
41  e.list : /*empty*/ =
42  e.new-list;
43
44Split s.Fname e.Fargs (e.list) =
45  () () e.list $iter {
46    e.list : t.item e.rest, {
47      <Apply s.Fname e.Fargs t.item> : e =
48        (e.true-list t.item) (e.false-list) e.rest;
49      (e.true-list) (e.false-list t.item) e.rest;
50    };
51  } :: (e.true-list) (e.false-list) e.list,
52  e.list : /*empty*/ =
53  (e.true-list) (e.false-list);
54
55Elem? e.list t.item =
56  e.list : e t.item e;
57
58Get-Elem s.num (e.list) = <L s.num e.list>;
59
60Foldr s.Fname e.Fargs (e.value) (e.list) =
61  (e.value) e.list $iter {
62    e.list : e.something t.last =
63      (<Apply s.Fname e.Fargs t.last e.value>) e.something;
64  } :: (e.value) e.list,
65  e.list : /*empty*/ =
66  e.value;
67
68Foldr1 s.Fname e.Fargs (e.list), {
69  e.list : e.something t.last =
70    <Foldr s.Fname e.Fargs (t.last) (e.something)>;
71  /*empty*/;
72};
73
74Concat e.lists = <Map &Deparen-Term (e.lists)>;
75
76Paren expr = <Map &Paren-Term (expr)>;
77
78Reverse {
79  term e.list = <Reverse e.list> term;
80  /*empty*/ = /*empty*/;
81};
82
83Intersperse (e.sep) e.list =
84  (/*e.new-list*/) e.list $iter {
85    e.list : t1 v2 =
86      (e.new-list t1 e.sep) v2;
87    (e.new-list e.list);
88  } :: (e.new-list) e.list,
89  e.list : /*empty*/ =
90  e.new-list;
91
92Nub e.expr =
93  e.expr () $iter {
94    e.expr : term e.rest, {
95      e.selected : e term e = e.rest (e.selected);
96      e.rest (e.selected term);
97    };
98  } :: e.expr (e.selected),
99  e.expr : /*empty*/ =
100  e.selected;
101
102Replicate s.n e.expr =
103  s.n /*e.list*/ $iter
104    <"-" s.n 1> e.list e.expr
105  :: s.n e.list,
106  <"<=" (s.n) (0)> =
107  e.list;
108
109/*
110 * Returns number of e.expr entries in e.source and e.source without all e.expr's.
111 */
112Entries e.expr (e.source) =
113  0 e.source () $iter {
114    e.source : e1 e.expr e2 = <"+" s.num 1> e2 (e.res e1);
115    s.num (e.res e.source);
116  } :: s.num e.source (e.res),
117  e.source : /*empty*/ =
118  s.num e.res;
119
120/*
121 * Add to the first list all new elements from the second one.
122 */
123Or (e.list1) e.list2 =
124  (e.list1) e.list2 $iter {
125    e.list2 : term e.rest,
126      {
127        e.list1 : e term e = (e.list1) e.rest;
128        (e.list1 term) e.rest;
129      };
130  } :: (e.list1) e.list2,
131  e.list2 : /*empty*/ =
132  e.list1;
133
134/*
135 * Choose from the first list all elements present in the second one.
136 */
137And (e.list1) e.list2 =
138  (e.list1) /*empty*/ $iter {
139    e.list1 : term e.rest,
140      {
141        e.list2 : e term e = (e.rest) e.and term;
142        (e.rest) e.and;
143      };
144  } :: (e.list1) e.and,
145  e.list1 : /*empty*/ =
146  e.and;
147
148/*
149 * Choose from the first list all elements not present in the second one.
150 */
151Sub (e.list1) e.list2 =
152  (e.list1) /*empty*/ $iter {
153    e.list1 : term e.rest,
154      {
155        e.list2 : e term e = (e.rest) e.not;
156        (e.rest) e.not term;
157      };
158  } :: (e.list1) e.not,
159  e.list1 : /*empty*/ =
160  e.not;
161
Note: See TracBrowser for help on using the repository browser.