Now try to solve the next problems using findall/3.
p(Graph,Node) :- member(Parent/Node,Graph), write(Parent), nl, fail. p(_Graph,_Node) :- write('end').
Recall Exercise 9 of Session 3:
During a student initiation ritual some years ago, several students got injured when they came in contact with a mix of 'daily use' products such as vinegar, oil,... . To prevent such accidents from happening in the future, you are asked to develop a system so that you can test whether a certain mixture is dangerous or not. To help you, a chemical expert gives you a database of mixtures of 'daily products' and numbers indicating how dangerous the mixtures are. The database is written as prolog facts:
reacts(vinegar,salt,25). reacts(salt,water,3). reacts('brown soap',water,10). reacts('pili pili', milk,7). reacts(tonic,bailey,8).Higher numbers are more dangerous. The order of the elements is not important: e.g. salt and vinegar react the same as vinegar and salt do. Now suppose that you can add the numbers: e.g. the number for a mixture of vinegar, salt and water is 25 (vinegar & salt) + 3 (salt & water) = 28. Also, suppose that a number less than 5 results in no irritation, between 6 and 12 results in minor irritation, between 13 and 20 results in minor burning wounds, between 21 and 30 results in severe burning wounds and higher values can be lethal.
?- advice([vinegar,salt,water]). Warning: this mixture causes severe burning wounds, never use this! Yes ?- advice([jam, salt, pepper, water, 'red onions', 'brown soap']). Warning: this mixture could result in minor burning wounds! Yes
Try to find a shorter solution than the solution of Session 3 by using findall/3.
Suppose you have some bottles of wine from three different wine farmers in total. For most bottles you know from which wine farmer they are (call these bottles the train data). However, for some of the bottles you do not know from which farmer they are (call these bottles the test data). Now you want to develop a system that classifies the bottles of wine from the test data: given the results of a chemical analysis of the wine in the bottle, the system should specify if this bottle is from farmer 1, 2 or 3.
We will solve this problem with a technique called Nearest Neightbours. If we want to classify a bottle b1 from an unknown farmer (a bottle from the test data), we can look for the bottle b2 in the train data that is most similar to bottle b1. We will then assume that b1 is from the same farmer as b2. This technique is called 1-Nearest Neighbour. We could also look for the 5 bottles in the train data that are most similar to bottle b1, check from which farmer most of these 5 bottles are and then assume that bottle b1 is also from that farmer. This is called 5-Nearest Neighbours.
To be able to use this technique, we need to have a measure of distance dist(b1,b2) between two bottles of wine b1 and b2. The bottle b2 that is 'most similar' to bottle b1 is then the bottle for which dist(b1,b2) is the smallest. More precisely, we will look at the distance between the chemical analysis of bottle b1 and the chemical analysis of bottle b2. We will represent the chemical analysis of a bottle of wine as a list of 13 numbers, each number indicating 1 chemical property.
WineData.pl contains 125 traindata/2 facts. Each
of these facts corresponds to 1 bottle for which we know the farmer. The first
argument of such a fact is always a chemical analysis (a list of 13 numbers), the second argument is
the farmer the bottle is from (1, 2 or 3).
WineData.pl also
contains 39 testdata/2 facts. Each of these facts corresponds
to 1 bottle for which we do not know the farmer. The first
argument of such a fact is always a bottle-number (b1 to b39), the second argument is a
chemical analysis.
The following two exercises are regular exercises on lists and do not involve findall !
Suppose we represent a LOA-board as a list of lists, where each of the inner lists
represents a row.
We use the atom b to indicate a black disk at a certain board position,
the atom w to indicate a white disk and
the atom o to indicate an open board position.
A LOA-board is 'valid' if and only if the board is square with width 8 and each
position is either open (o) or filled with a black disk (b) or
a white disk (w).