Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
26 lines (22 sloc) 718 Bytes
// P11 Modified run-length encoding.
import core.List;
import core.Pair;
import static core.List.*;
import static core.Pair.*;
class P11 {
// Recursion, list of objects
@SuppressWarnings("unchecked")
<T> List f1(List<Pair<Integer, T>> list) {
if (list.isEmpty()) return nil();
Pair<Integer, T> h = list.head();
return cons(h.getA().equals(1) ? h.getB() : h, f1(list.tail()));
}
// Imperative, list of objects
@SuppressWarnings("unchecked")
<T> List f2(List<Pair<Integer, T>> list) {
List acc = nil();
for (Pair<Integer, T> each : list)
acc = cons(each.getA().equals(1) ? each.getB() : each, acc);
return acc.reverse();
}
}