Created
January 9, 2016 11:52
-
-
Save miklund/0b418905ce6d25fc75a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Title: Infinite sequence of primes | |
| # Author: Mikael Lundin | |
| # Link: http://blog.mikaellundin.name/2011/08/13/infinite-sequence-of-primes-by-sequence-expressions.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let isPrime number primes = | |
| let sqrtn = float >> sqrt >> int | |
| primes | |
| |> Seq.takeWhile (fun n -> n <= (sqrtn number)) | |
| |> Seq.exists (fun n -> number % n = 0) | |
| |> not |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let rec primes = | |
| let isPrime number primes = | |
| let sqrtn = float >> sqrt >> int | |
| primes | |
| |> Seq.takeWhile (fun n -> n <= (sqrtn number)) | |
| |> Seq.exists (fun n -> number % n = 0) | |
| |> not | |
| let rec primes' current = | |
| seq { | |
| if primes |> isPrime current then | |
| yield current | |
| yield! primes' (current + 2) | |
| } | |
| seq { | |
| yield 2 | |
| yield! primes' 3 | |
| } |> Seq.cache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let rec primes' current = | |
| seq { | |
| if primes |> isPrime current then | |
| yield current | |
| yield! primes' (current + 2) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| seq { | |
| yield 0 | |
| yield! [1; 2; 3] | |
| yield! [4; 5; 6] | |
| yield! [7; 8; 9] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let rec alternating = | |
| seq { | |
| yield true | |
| yield false | |
| yield! alternating | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| seq { | |
| yield 2 | |
| yield! primes' 3 | |
| } |> Seq.cache |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment