Skip to content

Instantly share code, notes, and snippets.

@rand00
Created September 5, 2015 21:44
Show Gist options
  • Save rand00/944685800bed48a8ae6e to your computer and use it in GitHub Desktop.
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
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