Skip to content

Instantly share code, notes, and snippets.

@bjartwolf
Created May 10, 2015 08:12
Show Gist options
  • Save bjartwolf/6dda7a7587e11f121d91 to your computer and use it in GitHub Desktop.
Save bjartwolf/6dda7a7587e11f121d91 to your computer and use it in GitHub Desktop.
second
type symbol = Number of int | Plus of int * symbol | Minus of int * symbol | Noop of int * symbol
let merge (i1: int) (i2:int) = (int)(sprintf "%d%d" i1 i2)
let rec flattenNoop ((i,s): (int*symbol)) =
match s with
| Number n -> Number (merge i n)
| Plus (n,t) -> Plus (merge i n,t)
| Minus (n,t) -> Minus (merge i n,t)
| Noop (n,t) -> flattenNoop (merge i n,t)
let rec calc (s: symbol) : int =
match s with
| Number x -> x
| Plus (x,s) -> x + calc(s)
| Minus (x,s) -> x- calc(s)
| Noop (x,s) -> calc(flattenNoop (x, s))
let rec genNumbers (s:int) (stopp: int) = seq {
if s = stopp then yield Number s
else for i in genNumbers (s+1) stopp do
yield Plus (s,i)
yield Minus (s,i)
yield Noop (s,i)
}
genNumbers 1 9 |> Seq.filter (fun x -> 100 = calc x)|> Seq.distinct|> Seq.toList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment