Skip to content

Instantly share code, notes, and snippets.

@Gab-km
Created March 17, 2016 01:44
Show Gist options
  • Save Gab-km/43663bd5aba0cfa305fe to your computer and use it in GitHub Desktop.
Save Gab-km/43663bd5aba0cfa305fe to your computer and use it in GitHub Desktop.
何をやりたいか分からなくなってきたので放流する
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