Skip to content

Instantly share code, notes, and snippets.

@dingxiangfei2009
Created April 3, 2019 06:36
Show Gist options
  • Save dingxiangfei2009/83733e08fa9ab16b824e75acb594fb85 to your computer and use it in GitHub Desktop.
Save dingxiangfei2009/83733e08fa9ab16b824e75acb594fb85 to your computer and use it in GitHub Desktop.
let
rec' = hd: tl: self: args: { value = hd args; next = self (tl args); };
step = rec' ({ step, value }: value) ({ step, value }: { inherit step; value = value + step; });
nat2 = lib.fix step { step = 1; value = 2; };
subtract_sorted = self: xs: ys:
if xs.value < ys.value then
{ inherit (xs) value; next = self (xs.next) ys; }
else if xs.value > ys.value then
{ inherit (xs) value; next = self (xs.next) (ys.next); }
else
self (xs.next) (ys.next);
sieve = p: lib.fix step { value = p * p; step = p; };
prime_gen = self: candidates:
let
inherit (candidates) value;
in
{ inherit value; next = self (lib.fix subtract_sorted (candidates.next) (sieve value)); };
prime = lib.fix prime_gen nat2;
in
prime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment