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 KusoBuilder() = | |
member __.Bind(x, f) = f x | |
member __.Return(x) = x | |
member __.ReturnFrom(x) = x | |
member __.Using(x: #IDisposable, f) = try f x finally if x |> box |> isNull |> not then x.Dispose() | |
member __.Zero() = () | |
member __.Combine(_, f) = f() | |
member __.TryWith(f, g) = try f() with e -> g e |
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 totient n = | |
let rec totient isFirst (r, n) d = | |
match n % d with | |
| 0 -> totient false (r * (d - (if isFirst then 1 else 0)), n / d) d | |
| _ -> r, n | |
let nearlyPrimes = seq { yield 2; for i in 3 .. 2 .. n do yield i } | |
((1, n), nearlyPrimes) ||> Seq.fold (totient true) |> fst | |
let totientTest() = | |
let expected = [1; 1; 2; 2; 4; 2; 6; 4; 6; 4] |
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
(* | |
twiplaの参加者一覧のTSVファイルを作成します。 | |
使用方法 | |
twiplaの印刷用ページを表示してページ全体をクリップボードにコピーし、その内容を文字列にしてtwiplaToTsvの引数に渡します。 | |
*) | |
open System | |
let twiplaToTsv (content : string) = |
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 factorialNumberSystem n = | |
let n = n - 1 | |
let factorials = | |
if n = 0 then [1] | |
else | |
Seq.initInfinite ((+) 2) |> Seq.scan (*) 1 |> Seq.takeWhile ((>=) n) | |
|> Seq.toList |> List.rev | |
let factoradic k = |
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 r = System.Random() | |
let expected = [|0; 0; 0; 0; 1|] | |
let kiyoshi() = | |
Seq.initInfinite (fun _ -> r.Next 2) |> Seq.windowed 5 | |
|> Seq.takeWhile ((<>) expected) | |
|> Seq.map Array.head | |
|> Seq.append <| expected | |
|> Seq.iter (Array.get [|"ズン"; "ドコ"|] >> printf "%s") | |
printfn "キ・ヨ・シ!" |
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
// 単位の定義 | |
[<Measure>] type m | |
[<Measure>] type s | |
[<Measure>] type kg | |
[<Measure>] type 歳 | |
// 単位の別名 | |
[<Measure>] type meter = m | |
// 組立単位は単位を組み合わせて定義します。単位 N は kg * m / (s * s) の別名扱いです。 | |
[<Measure>] type N = kg * m / (s * s) |
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 lazy1 = lazy (printfn "hoge"; 1) | |
// val lazy1 : Lazy<int> = 値は作成されていません。 | |
// The Force Awakens | |
lazy1.Force() | |
// hoge | |
// val it : int = 1 | |
lazy1.Force() | |
// val it : int = 1 |
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 FSharp.Control.Reactive | |
open FSharp.Control.Reactive.Observable | |
open System | |
module FizzBuzzBase = | |
/// 100ミリ秒ごとに1, 2, 3, ...を発行 | |
let numberObservable = | |
TimeSpan.FromMilliseconds 100.0 |> interval |> map ((+) 1L) | |
|> publish |
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 fib = function | |
| 0 -> 0 | |
| 1 -> 1 | |
| n -> fib (n - 1) + fib (n - 2) | |
/// 素朴なフィボナッチ数列の実装(メモ化) | |
let fibMemo = | |
let memo = System.Collections.Generic.Dictionary(dict [0, 0; 1, 1]) | |
let rec fib n = |
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
type Word = TrueZoi of string | FalseZoi | TrueOtherwise of string | |
let isTrue = function TrueZoi _ | TrueOtherwise _ -> true | FalseZoi -> false | |
let toString = function TrueZoi x | TrueOtherwise x -> x | FalseZoi -> "ぞい" | |
let countZoi = function TrueZoi _ | FalseZoi -> 1 | TrueOtherwise _ -> 0 | |
let dic = | |
[ | |
[|TrueOtherwise "今日"; FalseZoi|] | |
[|TrueOtherwise "も"|] | |
[|TrueOtherwise "1"; FalseZoi|] |