mirror of
https://github.com/anoma/juvix.git
synced 2024-12-12 14:28:08 +03:00
544bddba43
- Closes #1731
40 lines
783 B
Plaintext
40 lines
783 B
Plaintext
-- streams without memoization
|
|
|
|
type list {
|
|
nil : list;
|
|
cons : any -> list -> list;
|
|
};
|
|
|
|
def force := \f f nil;
|
|
|
|
def filter := \p \s \_
|
|
case force s of {
|
|
cons h t :=
|
|
if p h then
|
|
cons h (filter p t)
|
|
else
|
|
force (filter p t)
|
|
};
|
|
|
|
def nth := \n \s
|
|
case force s of {
|
|
cons h t := if n = 1 then h else nth (n - 1) t
|
|
};
|
|
|
|
def numbers := \n \_ cons n (numbers (n + 1));
|
|
|
|
def indivisible := \n \x if x % n = 0 then false else true;
|
|
def eratostenes := \s \_
|
|
case force s of {
|
|
cons n t :=
|
|
cons n (eratostenes (filter (indivisible n) t))
|
|
};
|
|
def primes := eratostenes (numbers 2);
|
|
|
|
def writeLn := \x write x >> write "\n";
|
|
|
|
writeLn (nth 100 primes) >>
|
|
writeLn (nth 1000 primes) >>
|
|
writeLn (nth 10000 primes) >>
|
|
writeLn (nth 20000 primes)
|