Prolog Programming. 2 DATA STRUCTURES IN PROLOG PROGRAMMING TECHNIQUES CONTROL IN PROLOG CUTS.
Prolog Extra Credit Homework
description
Transcript of Prolog Extra Credit Homework
Prolog Extra Credit Homework
Basic Boolean Operators in Prolog
or(0,0,0).or(0,1,1).or(1,0,1).or(1,1,1).
and(0,0,0)and(0,1,0).and(1,0,0).and(1,1,1).
not(0,1).not(1,0).
xor(0,0,0).xor(0,1,1).xor(1,0,1).xor(1,1,0).
Half-adder:
looks a lot like?
xor() and()
halfadder(A,B,Carry,Sum):- xor(A,B,Sum),and(A,B,Carry).
Full-adder:
or-gate
Exercise: Write the prologrule for a full adder.
fulladder(A,B,CarryIn,Sum,CarryOut):- halfadder(A,B,S,C1),halfadder(S,CarryIn,Sum,C2),or(C1,C2,CarryOut).
Programming Problem 1:
Write a prolog program that will combine one half-adder and three full-adders to perform 4-bit arithmetic.
add4bits([A3,A2,A1,A0],[B3,B2,B1,B0],[Sum3,Sum2,Sum1,Sum0],CarryOut):-
these are lists
Programming Problem 2:
A 4-input multiplexer accepts4 input bits and outputsexactly one of them.
The bit to be outputteddepends on the values of the control lines - C0 and C1.
For example, if C0 = 0 and C1 = 1 then Output = D2.
4-bit Multiplexer Logic:
and-gate
or-gate
complementor
multiplex4bits([D3,D2,D1,D0],[C1,C0],Output):- ...