Created
September 5, 2015 21:44
-
-
Save rand00/944685800bed48a8ae6e to your computer and use it in GitHub Desktop.
Solution to Paul Grahams accumulator generator problem, for OCaml.. http://www.paulgraham.com/accgen.html
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
module Num = struct | |
type t = [ `F of float | `I of int ] | |
let to_float = function | |
`F f -> f | |
| `I i -> float i | |
let add n n' = match n, n' with | |
`F f, `F f' -> `F (f +. f') | |
| `I i, `I i' -> `F (float (i + i')) | |
| `F f, `I i | `I i, `F f -> `F ((float i) +. f) | |
let accgen n = | |
let r = ref n in fun i -> r := add !r i; !r | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment