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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
nombre(N):-member(N,[0,1,2,3,4,5,6,7,8,9]).
cherche([W,X,Y,Z]):-nombre(W),nombre(X),nombre(Y),nombre(Z),15 is W + X + Y + Z ,9 is W + Z,12 is W * X,15 is Y * Z.
% Checks so that the user doesn't try and do things he or she shouldn't
check(nombre(_)).
check(cherche(_)).
check('!').
go(Get) :-
% Get the query from the GET request
member(q=Query,Get),
% Convert to a term
catch(atom_to_term(Query, Term, Bindings),_,fail),
nonvar(Term),
% Check user has supplied a valid query
check(Term),
!,
% Find all solutions
forall(Term, (bind(Bindings), write(Term), nl)),
!.
% This binds a Variable=Name list so that unbound variables are bound to their Name
bind([]).
bind([A=A|T]) :-
!,
bind(T).
bind([_|T]) :-
bind(T).
/*<html>
<head>
<title>PSP Demo</title>
</head>
<body>
<a href="template.prolog?t=psp#psp">Back</a><br>
<a href="family.html">View Source</a><br><br>
<!-- Just show the database, plain old text -->
Database:
<pre>
nombre(N):-member(N,[0,1,2,3,4,5,6,7,8,9]).
cherche([W,X,Y,Z]):-nombre(W),nombre(X),nombre(Y),nombre(Z),
15 is W + X + Y + Z ,
9 is W + Z,
12 is W * X,
15 is Y * Z.
</pre>
<!-- Work out what the query was -->
<?, (member(q=Query,Get);Query='No Query Supplied') ,?>
<!-- Show the query -->
Question:
<pre>
<?= Query ?>
</pre>
<!-- Show the solutions -->
Solutions:
<pre>
<?, once(
go(Get)
;
?>Error: Invalid Term (The query must be a valid prolog query)<?
)
,?></pre>
<!-- And have an input box for the next query -->
<form method="GET" action="essais.prolog">
New Query: <input type="text" name="q"><br>
<input type="submit">
</form>
Exemples de questions:
<pre>
cherche(N).
</pre>
</body>
</html>*/ |
Partager