Created
May 10, 2015 11:31
-
-
Save bjartwolf/1209c36958774d8eb642 to your computer and use it in GitHub Desktop.
typebased
This file contains hidden or 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
type symbol = Number of int | Plus of int * symbol | Minus of int * symbol | Join of int * symbol | |
let merge (i1: int) (i2:int) = (int)(sprintf "%d%d" i1 i2) | |
let rec flattenJoin ((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) | |
| Join (n,t) -> flattenJoin (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) | |
| Join (x,s) -> calc(flattenJoin (x, s)) | |
let rec genSolutions (s:int) (stopp: int) = seq { | |
if s = stopp then yield Number s | |
else for i in genSolutions (s+1) stopp do | |
yield Plus (s,i) | |
yield Minus (s,i) | |
yield Join (s,i) | |
} | |
let rec prettyPrint (s:symbol) :string = | |
match s with | |
| Number n -> sprintf "%d" n | |
| Join (n,t) -> sprintf "%d" n + prettyPrint t | |
| Plus (n,t) -> sprintf "%d" n + "+" + prettyPrint t | |
| Minus (n,t) -> sprintf "%d" n + "-" + prettyPrint t | |
genSolutions 1 9 |> Seq.filter (fun x -> 100 = calc x) |> | |
Seq.map((prettyPrint)) |> Seq.toList |> printfn "%A" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment