Skip to content

Instantly share code, notes, and snippets.

@hkoba
Created May 19, 2013 03:23
Show Gist options
  • Save hkoba/5606543 to your computer and use it in GitHub Desktop.
Save hkoba/5606543 to your computer and use it in GitHub Desktop.
Short prolog util to emit (only parameter part of) truth table. (See inline comments)
% -*- mode: prolog; coding: utf-8 -*-
% Simple util to make truth table.
% tested under gnu prolog and swi prolog.
% ?- consult('truthtable1.pl').
% | ?- tt([[y,n], [y,n]], writeln).
% [y,y]
% [n,y]
% [y,n]
% [n,n]
%
% no
% | ?- tt([[y,n], [y,n], [y,n]], write_csv).
% y y y
% n y y
% y n y
% n n y
% y y n
% n y n
% y n n
% n n n
%
% no
tt(List, Call) :-
tt_q(List, [], Call), fail.
tt_q([], Result, Call) :-
call(Call, Result).
tt_q([Hques|Tques], R, Call) :-
tt_ans(Hques, Tques, R, Call).
tt_ans([Hans|Tans], Tques, R, Call) :-
tt_q(Tques, [Hans | R], Call),
!,
(Tans = []; tt_ans(Tans, Tques, R, Call)).
writeln(X) :-
write(X), nl.
write_csv([]) :-
nl, true.
write_csv([H|T]) :-
write(H), (T = []; write('\t')), write_csv(T).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment