Files
dholle 2e5447c06b
SonarQube Scan / SonarQube Trigger (push) Failing after 2m17s
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 1m44s
Add Primzahlen, fixed a few problems
2026-09-10 16:22:00 +02:00

54 lines
1.6 KiB
Java

import java.lang.Integer;
import java.lang.String;
import java.lang.System;
import java.lang.Boolean;
import java.io.PrintStream;
import java.util.function.Supplier;
import java.util.function.Function;
import LazyList;
import Cons;
import Empty;
public class Primzahlen {
static LazyList from(int i) { return new Cons(i, () -> from(i+1)); }
LazyList filter(Function<Integer, Boolean> p, LazyList l) {
return switch (l) {
case Empty() -> l;
case Cons(Integer x, Supplier<LazyList> l1) ->
p.apply(x) ? new Cons(x, () -> filter(p, l1.get())) : filter(p, l1.get());
};
};
LazyList dropMul(Integer x, LazyList xs) {
return filter(y -> (y % x != 0), xs);
}
LazyList del(LazyList xs) {
return new Cons(xs.fst(), () -> del(dropMul(xs.fst(), xs.rest())));
}
LazyList primes() {
return del(from(2));
}
LazyList take(Integer n, LazyList l) {
if (n == 0) return new Empty();
else return switch (l) {
case Empty() -> l;
case Cons(Integer x, Supplier<LazyList> l1) ->
new Cons(x, () -> take(n-1, l1.get()));
};
};
public static void main(args) {
System.out.println(new Cons(1, () -> new Cons(2, () -> new Empty())).fst());
LazyList l = new Cons(1, () -> new Cons(2, () -> new Cons(2, () -> new Empty())));
System.out.println(new Primzahlen().filter(x -> x == 2, l));
System.out.println(from(1).rest().fst());
Primzahlen pz = new Primzahlen();
System.out.println(pz.take(10, pz.from(2)));
System.out.println(pz.take(14, pz.primes()));
}
}