Bai tap AI,
Trang 1Tut1_Sol Question 1
get3rd([_, _, I | _], I)
insertLast(H, L, R) :- append(L, [H], R)
getN([H | _], 1, H) :- !
getN([_ | T], N, X) :- N1 is N - 1, getN(T, N1, X)
append2l([], X, X)
append2l([H | T], X, [H | T1]) :- append2l(T, X, T1)
reverseL([], [])
reverseL([H | T], L) :- reverseL(T, T1), append(T1, [H],L)
Question 2
prefix([],[_|_])
prefix([X|T1],[X|T2]) :- prefix(T1,T2)
postfix(L1,L2) :- reverseL(L1,T1),reverseL(L2,T2),prefix(T1,T2)
Question 3
separate([],[],[])
separate([H|T],[H|L1], L2):-H mod 2 < 1,!, separate(T,L1,L2)
separate([H|T],L1,[H|L2]):-separate(T,L1,L2)
Question 4
check([])
check([_])
check([_, _])
check([E1, E2, E3]) :- E3 > E1 * E2
check([H1, H2, H3, H4 | T]) :- H3 > H1 * H2, H4 < H2 * H3, check([H3, H4 | T])
Question 5
remoDup([H], [H]) :- !
remoDup([H | [H | T]], [H | T1]) :- remoDup([H | T], [H | T1]), !
remoDup([H | [H1 | T]], [H | [H1 | T1]]) :- remoDup([H | T], [H | T1])
change([], []) :- !
change([H | T], [H | T1]) :- remoDup([H | T], [H | T2]), change(T2, T1) Question 6
Page 1
Trang 2Tut1_Sol calculate(0, N, X) :- X is N + 1, !
calculate(M, 0, X) :- M1 is M - 1, calculate(M1, 1, X), !
calculate(M, N, X) :- M1 is M - 1, N1 is N - 1, calculate(M, N1, X1), calculate(M1, X1, X)
Page 2