%%% Question 1: The following predicates tests whether a library structure contains %%% a book by Dumas p([book(dumas,_,_,_) | _]). p([_|Rest]):- p(Rest). %%% Question 2: Extracting titles titles([],[]). titles( [book(_, Title, _, _)|More], [Title|MoreTitles]):- titles(More,MoreTitles). %%% Question 3: Extracting all detective stories detectiveStories([],[]). detectiveStories( [book(Aut, Title, Pages, detective)|More], [book(Aut, Title, Pages, detective)|MoreDetectives]):- detectiveStories(More,MoreDetectives). detectiveStories( [book(_, _, _, NoDetective)|More], MoreDetectives):- dif(NoDetective,detective), detectiveStories(More,MoreDetectives). %%%% Question 4: Vacation reading % Clause 1: the empty library is OK vacationReading([],[],0). % Clause 2: If I can include one more non-study book without exceeding 800 pp, it is OK. vacationReading([book(Aut, Title, Pages, Category)|MoreBooks], [book(Aut, Title, Pages, Category)|MoreVacationBooks], TotalPages):- dif(Category,study), vacationReading(MoreBooks,MoreVacationBooks, MorePages), TotalPages is MorePages+Pages, TotalPages =< 800. % Clause 3: I can decide to leave any book at home, independent of which kind of book vacationReading([_|MoreBooks], VacationBooks, Pages):- vacationReading(MoreBooks,VacationBooks,Pages). test_data([ book(sayers, someTitle, 288, detective), book(negnevitski, artificialIntelligence, 415, study), book(nerdson, smartAlgorithms, 465, study), book(sayers, someOtherTitle, 194, detective), book(dumas, musqueteers, 777, justFun) ]). /***********************************tests ?- test_data(Lib), titles(Lib,Titles). Lib = ....., Titles = [someTitle,artificialIntelligence,smartAlgorithms,someOtherTitle,musqueteers] ? ; no ?- test_data(Lib), detectiveStories(Lib,Ds). Ds = [book(sayers,someTitle,288,detective),book(sayers,someOtherTitle,194,detective)], Lib = ..... ? ; no ?- test_data(Lib), vacationReading(Lib,VR,N). N = 482, VR = [book(sayers,someTitle,288,detective),book(sayers,someOtherTitle,194,detective)], Lib = ..... ? ; N = 288, VR = [book(sayers,someTitle,288,detective)], Lib = ..... ? ; N = 194, VR = [book(sayers,someOtherTitle,194,detective)], Lib = ..... ? ; N = 777, VR = [book(dumas,musqueteers,777,justFun)], Lib = ..... ? ; N = 0, VR = [], Lib = ..... ? ; no *********************/