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

Last change on this file since 1830 was 1830, checked in by orlov, 15 years ago
  • <Intersperse> function has been added; <Concat> has been improved.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1// $Source$
2// $Revision: 1830 $
3// $Date: 2006-01-19 01:03:25 +0000 (Thu, 19 Jan 2006) $
4
5$use Apply Access Arithm;
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
102/*
103 * Returns number of e.expr entries in e.source and e.source without all e.expr's.
104 */
105Entries e.expr (e.source) =
106  0 e.source () $iter {
107    e.source : e1 e.expr e2 = <"+" s.num 1> e2 (e.res e1);
108    s.num (e.res e.source);
109  } :: s.num e.source (e.res),
110  e.source : /*empty*/ =
111  s.num e.res;
112
113/*
114 * Add to the first list all new elements from the second one.
115 */
116Or (e.list1) e.list2 =
117  (e.list1) e.list2 $iter {
118    e.list2 : term e.rest,
119      {
120        e.list1 : e term e = (e.list1) e.rest;
121        (e.list1 term) e.rest;
122      };
123  } :: (e.list1) e.list2,
124  e.list2 : /*empty*/ =
125  e.list1;
126
127/*
128 * Choose from the first list all elements present in the second one.
129 */
130And (e.list1) e.list2 =
131  (e.list1) /*empty*/ $iter {
132    e.list1 : term e.rest,
133      {
134        e.list2 : e term e = (e.rest) e.and term;
135        (e.rest) e.and;
136      };
137  } :: (e.list1) e.and,
138  e.list1 : /*empty*/ =
139  e.and;
140
141/*
142 * Choose from the first list all elements not present in the second one.
143 */
144Sub (e.list1) e.list2 =
145  (e.list1) /*empty*/ $iter {
146    e.list1 : term e.rest,
147      {
148        e.list2 : e term e = (e.rest) e.not;
149        (e.rest) e.not term;
150      };
151  } :: (e.list1) e.not,
152  e.list1 : /*empty*/ =
153  e.not;
154
Note: See TracBrowser for help on using the repository browser.