Created
March 17, 2016 01:44
-
-
Save Gab-km/43663bd5aba0cfa305fe to your computer and use it in GitHub Desktop.
何をやりたいか分からなくなってきたので放流する
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 factorial n = | |
match n with | |
| 0 | 1 -> 1 | |
| _ -> n * (factorial (n - 1)) | |
type FactorialNumber(num: int) = | |
let infactorials = Seq.initInfinite <| fun i -> factorial (i+1) | |
let range = Seq.takeWhile (fun fc -> fc <= num) infactorials |> Seq.toList | |
let rec factSystemize n fcs acc = | |
match fcs with | |
| fc::rest -> | |
let quotient = n / fc | |
let remainder = n % fc | |
factSystemize remainder rest (quotient::acc) | |
| [] -> acc | |
member val Value = num with get | |
member val Factorials = factSystemize num (List.rev range) [] with get | |
member self.Add (other:FactorialNumber) = FactorialNumber(self.Value + other.Value) | |
static member (+) (self: FactorialNumber, other: FactorialNumber) = self.Add(other) | |
type FactorialNumberSystemBuilder() = | |
member __.Run(fn: FactorialNumber) = fn.Factorials | |
member __.Return(x) = FactorialNumber(x) | |
member __.Bind(x: FactorialNumber, f) = f (x.Value) | |
let fns = FactorialNumberSystemBuilder() | |
fns { | |
let x = 10 | |
let y = 17 | |
return x + y | |
} // [1; 1; 0; 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment