This file contains 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 (|Equals|_|) x y = | |
if x = y then Some () else None | |
let (|Id|_|) f x = | |
if f x then Some () else None | |
let a = 3;; | |
match 3 with | |
| Equals a -> "Yes" |
This file contains 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 fn () = failwith "Test";; | |
async { | |
try | |
fn () | |
with e -> printfn "Catched %A" e | |
} |> Async.RunSynchronously |
This file contains 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
open System | |
open System.IO | |
open System.Net | |
type Request = Request of WebRequest * AsyncReplyChannel<WebResponse> | |
let requestAgent = | |
MailboxProcessor.Start <| fun inbox -> async { | |
while true do | |
let! (Request (req, port)) = inbox.Receive () |
This file contains 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 (|Negative|Positive|Zero|) x = | |
if x > 0 then Positive | |
else if x < 0 then Negative | |
else Zero | |
let count = -2;; | |
match count with | |
| Zero | Negative -> printfn "Zero or less" | |
| Positive -> printfn "Greater than zero" |
This file contains 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
open System | |
type IParallelLimiter = | |
abstract GetToken : unit -> Async<IDisposable> | |
type Message= | |
| GetToken of AsyncReplyChannel<IDisposable> | |
| Release | |
let start count = |
This file contains 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 getPrimeFactors n = | |
let allFactors = seq { yield 2; yield! Seq.initInfinite (fun i -> 2 * i + 3) } |> Seq.cache | |
let addFactor factors d = | |
match Map.tryFind d factors with | |
| None -> Map.add d 1 factors | |
| Some count -> Map.remove d factors |> Map.add d (count + 1) | |
let rec gPF n factors = | |
allFactors | |
|> Seq.takeWhile (fun d -> d <= (n |> float |> sqrt |> int)) | |
|> Seq.tryFind (fun d -> n % d = 0) |
This file contains 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 fibf = Seq.cache <| seq { | |
yield! [1I; 1I] | |
yield! Seq.map2 (+) fibf (Seq.skip 1 fibf) | |
};; | |
fibf |> Seq.nth 100000 |
This file contains 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 source = | |
let src = @"73167176531330624919225119674426574742355349194934 | |
96983520312774506326239578318016984801869478851843 | |
85861560789112949495459501737958331952853208805511 | |
12540698747158523863050715693290963295227443043557 | |
66896648950445244523161731856403098711121722383113 | |
62229893423380308135336276614282806444486645238749 | |
30358907296290491560440772390713810515859307960866 | |
70172427121883998797908792274921901699720888093776 | |
65727333001053367881220235421809751254540594752243 |
This file contains 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 n = | |
primes | |
|> Seq.takeWhile (fun v -> v <= (float n |> sqrt |> int64)) | |
|> (not << Seq.exists (fun v -> n % v = 0L)) | |
let rec primes' n = seq { | |
if isPrime n then | |
yield n | |
yield! primes' (n + 2L) | |
} |
This file contains 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 sumOfSquares n = | |
[1..n] | |
|> Seq.map (fun x -> x * x) | |
|> Seq.sum | |
let squareOfSum n = | |
let v = [1..n] |> Seq.sum | |
v * v | |
let diff n = |