Created
May 10, 2015 11:31
-
-
Save bjartwolf/8be43c71950dacd221a7 to your computer and use it in GitHub Desktop.
list solution
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 | Minus | |
let merge (i1: int) (i2:int) = (int)(sprintf "%d%d" i1 i2) | |
let rec calc (s: symbol list) : int = | |
match s with | |
| [] -> 0 | |
| [Number x] -> x | |
| Number x :: Number y :: rest -> calc(Number (merge x y) :: rest) | |
| Number x :: rest -> x + calc(rest) | |
| Plus :: rest -> + calc(rest) | |
| Minus :: rest -> - calc(rest) | |
let rec genSolutions (start: int) (stop: int) = seq { | |
if start = stop then yield [Number stop] | |
else for n in start .. start do | |
for i in genSolutions (start + 1) stop do | |
yield Number n :: i | |
yield Number n :: Minus :: i | |
yield Number n :: Plus:: i | |
} | |
let rec prettyPrint (s:symbol list) :string = | |
match s with | |
| [] -> "" | |
| Number n :: rest -> n.ToString() + prettyPrint rest | |
| Plus :: rest -> "+" + prettyPrint rest | |
| Minus :: rest -> "-" + prettyPrint rest | |
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