Last active
August 29, 2015 14:20
-
-
Save bjartwolf/0d702e5638025b0b6bcd to your computer and use it in GitHub Desktop.
initial
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 rec calc (s: symbol list) : int = | |
match s with | |
| [] -> 0 | |
| [Number x] -> x | |
| Number x :: Number y :: rest -> calc(Number ((int)(sprintf "%d%d" x y)) :: rest) | |
| Number x :: rest -> x + calc(rest) | |
| Plus :: rest -> + calc(rest) | |
| Minus :: rest -> - calc(rest) | |
let rec genPossibleSolutions (start: int) (stop: int) = seq { | |
if start = stop then yield [Number stop] | |
else for n in start .. start do | |
for i in genPossibleSolutions (start + 1) stop do | |
yield Number n :: i | |
yield Number n :: Minus :: i | |
yield Number n :: Plus:: i | |
} | |
genPossibleSolutions 1 9 |> Seq.filter (fun x -> 100 = calc x) |> Seq.toList | |
|> List.length |> printfn "Is %d long" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment