% % twentyfour.pl -- the 24 problem in prolog % % Submission site id(s): % % Comments: % some "nice" prolog settings... see assignment 8's % description for the details on what these do % -- but it's not crucial to know the details of these Prolog internals :- set_prolog_flag( prompt_alternatives_on, groundness ). :- set_prolog_flag(toplevel_print_options, [quoted(true), portray(true), attributes(portray), max_depth(999), priority(699)]). % here are several predicates that may help with this problem % (we wrote/looked at these together in class) % remove an element (which much be present) from a list... removeOne(X,[X|R],R). removeOne(X,[F|R],[F|S]) :- removeOne(X,R,S). % generates permutations of the first input's list perm([X|Y],Z) :- perm(Y,W), removeOne(X,Z,W). perm([],[]). % splits the first argument's list into subsets, i.e., % sublists in which the order of elements may vary split( List, A, B ) :- perm( List, L2 ), append( A, B, L2 ). % note that A and/or B can be [] here! % this is the eval predicate that evaluates our "24-trees" % the "nonvar" checks make sure that A and B are both bound to values % this prevents some prolog implementations of searching for bindings % for A and B (we only want to search for R). % eval(R, R) :- number(R). eval(['+', A, B], R) :- nonvar(A), nonvar(B), eval(A, AR), eval(B, BR), R is AR + BR. eval(['*', A, B], R) :- nonvar(A), nonvar(B), eval(A, AR), eval(B, BR), R is AR * BR. eval(['-', A, B], R) :- nonvar(A), nonvar(B), eval(A, AR), eval(B, BR), R is AR - BR. eval(['/', A, B], R) :- nonvar(A), nonvar(B), eval(A, AR), eval(B, BR), BR\==0, R is AR // BR. % You will need to write the solve predicate here: % % solve(Ops, Values, Result, Tree) %