-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprob.erl
28 lines (22 loc) · 846 Bytes
/
prob.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-module(prob).
-export([answer/0]).
%% You have the numbers 123456789, in that order. Between each number, you must
%% insert either nothing, a plus sign, or a multiplication sign, so that the
%% resulting expression equals 2002. Operator precedence does apply.
answer() -> find(lists:seq($1,$9)).
find(L) -> build(L, []).
build([], E) -> Ex = lists:reverse([$., $2, $0, $0, $2, $=,$:,$= | E]),
case eval(Ex) of
{value, true, _} ->
io:format("E: ~p~n", [Ex]);
_Else -> false
end;
build([H|T], []) -> build(T, [H]);
build([H|T], E) ->
build(T, [H, $+ | E]),
build(T, [H, $* | E]),
build(T, [H | E]).
eval(S) ->
{ok,Scanned,_} = erl_scan:string(S),
{ok,Parsed} = erl_parse:parse_exprs(Scanned),
erl_eval:exprs(Parsed,[]).