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

Last change on this file since 2044 was 2044, checked in by orlov, 14 years ago
  • Improved Intersperse.
  • 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: 2044 $
3// $Date: 2006-08-01 17:31:01 +0000 (Tue, 01 Aug 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 {
84  (e.sep) /*empty*/ = /*empty*/;
85  (e.sep) t1 = t1;
86  (e.sep) t1 e.list =
87    (t1 e.sep) e.list $iter {
88      e.list : t2 e.rest =
89        (e.new-list t2 e.sep) e.rest;
90    } :: (e.new-list) e.list,
91    e.list : t2 =
92    e.new-list t2;
93};
94
95Nub e.expr =
96  e.expr () $iter {
97    e.expr : term e.rest, {
98      e.selected : e term e = e.rest (e.selected);
99      e.rest (e.selected term);
100    };
101  } :: e.expr (e.selected),
102  e.expr : /*empty*/ =
103  e.selected;
104
105Replicate s.n e.expr =
106  s.n /*e.list*/ $iter
107    <"-" s.n 1> e.list e.expr
108  :: s.n e.list,
109  <"<=" (s.n) (0)> =
110  e.list;
111
112/*
113 * Returns number of e.expr entries in e.source and e.source without all e.expr's.
114 */
115Entries e.expr (e.source) =
116  0 e.source () $iter {
117    e.source : e1 e.expr e2 = <"+" s.num 1> e2 (e.res e1);
118    s.num (e.res e.source);
119  } :: s.num e.source (e.res),
120  e.source : /*empty*/ =
121  s.num e.res;
122
123/*
124 * Add to the first list all new elements from the second one.
125 */
126Or (e.list1) e.list2 =
127  (e.list1) e.list2 $iter {
128    e.list2 : term e.rest,
129      {
130        e.list1 : e term e = (e.list1) e.rest;
131        (e.list1 term) e.rest;
132      };
133  } :: (e.list1) e.list2,
134  e.list2 : /*empty*/ =
135  e.list1;
136
137/*
138 * Choose from the first list all elements present in the second one.
139 */
140And (e.list1) e.list2 =
141  (e.list1) /*empty*/ $iter {
142    e.list1 : term e.rest,
143      {
144        e.list2 : e term e = (e.rest) e.and term;
145        (e.rest) e.and;
146      };
147  } :: (e.list1) e.and,
148  e.list1 : /*empty*/ =
149  e.and;
150
151/*
152 * Choose from the first list all elements not present in the second one.
153 */
154Sub (e.list1) e.list2 =
155  (e.list1) /*empty*/ $iter {
156    e.list1 : term e.rest,
157      {
158        e.list2 : e term e = (e.rest) e.not;
159        (e.rest) e.not term;
160      };
161  } :: (e.list1) e.not,
162  e.list1 : /*empty*/ =
163  e.not;
164
Note: See TracBrowser for help on using the repository browser.