/** * Peter Mowry * Prolog lab2 * Finally have 9.17 working */ /** * 9.17 - A Parse Tree Grammar */ s(s(NP,VP)) --> np(NP), vp(VP). %s(X, Y) :- np(X, U), vp(U, Y). np(np(DET,N)) --> det(DET), n(N). %np(X, Y) :- det(X, U), n(U, Y). vp(vp(IV)) --> iv(IV). %vp(X, Y) :- iv(X, Y). vp(vp(TV,NP)) --> tv(TV), np(NP). %vp(X, Y) :- tv(X, U), np(U, Y). det(det(the)) --> [the]. %det([the | Y], Y, Z). det(det(a)) --> [a]. %det([a | Y], Y, Z). n(n(giraffe)) --> [giraffe]. %n([giraffe |Y], Y, Z). n(n(apple)) --> [apple]. %n([apple | Y], Y, Z). iv(iv(dreams)) --> [dreams]. %iv([dreams | Y], Y, Z). tv(tv(eats)) --> [eats]. %tv([eats | Y], Y, Z). %consult('lab2'). %s(Tree,[the, giraffe, eats, the, apple],[]). %s(Tree,[the, giraffe, dreams],[]). /** * 9.25 - Median Element in a List */ % bsort from pg - 269 in Principles and Paradigms % Duplicates are allowed bsort(L, S) :- append(U, [A, B | V], L), B < A, !, append( U, [B, A | V], M ), bsort( M, S ). bsort(L, L). % Base case of 1 median([M], M). % Base case of 2 median([X, Y | [] ], A) :- A is ((X + Y)/2). % Sort and Reverse median( X, Y ) :- bsort( X, [H1 | T1] ), reverse( T1, [H2 | T2] ), median( T2, Y ). /** * 9.26 * Is Mason blond? This is unspecified in my answer, but * this is ok b/c 26 only wants blond, handsome, unscarred. * The man is clearly steve, in all 8 versions of the answer */ adjacent(X, Y) :- X =:= Y+1. adjacent(X, Y) :- X =:= Y-1. % number, name, blond, handsome, scarred lineups([lineup(1,_,_,_,_), lineup(2,_,_,_,_), lineup(3,_,_,_,_), lineup(4,_,_,_,_)]). % One of them is blond, handsome, unscarred % two non-blond standing both sides of mason (A % Alex is the only one standing next to exactly one handsome man (B % Steve is the only one not standing next to exactly one scarred man (B again % Who is blond, handsome, and unscarred? lineup(Positions) :- lineups(Positions), member(lineup(_,_,blond,handsome,unscarred),Positions), member(lineup(A1,_,unblond,_,_),Positions), member(lineup(A2,_,unblond,_,_),Positions), member(lineup(B6,mason,_,_,_),Positions), (B6 = 2 | B6 = 3), adjacent(A1, B6), adjacent(A2, B6), A1 \= A2, member(lineup(1,_,_,B1,C1),Positions), member(lineup(2,_,_,B2,C2),Positions), member(lineup(3,_,_,B3,C3),Positions), member(lineup(4,_,_,B4,C4),Positions), member(lineup(B5,alex,_,_,_),Positions), member(lineup(B7,steve,_,_,_),Positions), member(lineup(B8,simon,_,_,_),Positions), % Alex (in B5) is the only one standing next to exactly one handsome man adj1h(B5,B1,B2,B3,B4), nadj1h(B6,B1,B2,B3,B4), nadj1h(B7,B1,B2,B3,B4), nadj1h(B8,B1,B2,B3,B4), % Steve (in B7) is the only one not standing next to exactly one scarred man (C nadj1s(B7,C1,C2,C3,C4), adj1s(B5,C1,C2,C3,C4), adj1s(B6,C1,C2,C3,C4), adj1s(B8,C1,C2,C3,C4). % adj1h = adjacent to one handsome adj1h(X,B1,B2,B3,B4) :- (X =:= 1, B2 = handsome) | (X =:= 4, B3 = handsome) | (X =:= 2, ( (B1 = handsome, B3 = unhandsome) | (B1 = unhandsome, B3 = handsome) )) | (X =:= 3, ( (B2 = handsome, B4 = unhandsome) | (B2 = unhandsome, B4 = handsome) )). % nadj1h = not adjacent to one handsome nadj1h(X,B1,B2,B3,B4) :- (X =:= 1, B2 = unhandsome) | (X =:= 4, B3 = unhandsome) | (X =:= 2, ( (B1 = handsome, B3 = handsome) | (B1 = unhandsome, B3 = unhandsome) )) | (X =:= 3, ( (B2 = handsome, B4 = handsome) | (B2 = unhandsome, B4 = unhandsome) )). % adj1s = adjacent to one scarred adj1s(X,C1,C2,C3,C4) :- (X =:= 1, C2 = scarred) | (X =:= 4, C3 = scarred) | (X =:= 2, ( (C1 = scarred, C3 = unscarred) | (C1 = unscarred, C3 = scarred) )) | (X =:= 3, ( (C2 = scarred, C4 = unscarred) | (C2 = unscarred, C4 = scarred) )). % nadj1s = notadjacent to one scarred nadj1s(X,C1,C2,C3,C4) :- (X =:= 1, C2 = unscarred) | (X =:= 4, C3 = unscarred) | (X =:= 2, ( (C1 = scarred, C3 = scarred) | (C1 = unscarred, C3 = unscarred) )) | (X =:= 3, ( (C2 = scarred, C4 = scarred) | (C2 = unscarred, C4 = unscarred) )). /** * 9.28 */ /* % first names runners([runner(harriet,_,_,_,_), runner(harvey,_,_,_,_), runner(larry,_,_,_,_), runner(paula,_,_,_,_), runner(steve,_,_,_,_)]). % last names runners([runner(_,dickerson,_,_,_), runner(_,gould,_,_,_), runner(_,kendrig,_,_,_), runner(_,simms,_,_,_), runner(_,whitley,_,_,_)]). */ % times runners([runner(_,_,150,_,_), runner(_,_,155,_,_), runner(_,_,160,_,_), runner(_,_,165,_,_), runner(_,_,170,_,_)]). /* % towns runners([runner(_,_,_,brunswick,_), runner(_,_,_,quincy,_), runner(_,_,_,northhampton,_), runner(_,_,_,providence,_), runner(_,_,_,williamsburg,_)]). % shirt colors runners([runner(_,_,_,_,blue), runner(_,_,_,_,green), runner(_,_,_,_,purple), runner(_,_,_,_,red), runner(_,_,_,_,yellow)]). */ % first_name, last_name, time, town, shirt_color marathon(Runners) :- runners(Runners), member(runner(harriet,_,A1,_,_),Runners), member(runner(harvey,_,_,williamsburg,I2),Runners), member(runner(_,simms,D1,_,_),Runners), member(runner(paula,_,D2,_,_),Runners), member(runner(_,_,_,quincy,red),Runners), member(runner(_,dickerson,F2,_,B),Runners), member(runner(steve,_,F3,_,_),Runners), member(runner(_,_,F4,northhampton,_),Runners), member(runner(_,_,F5,brunswick,I1),Runners), member(runner(_,_,F6,quincy,_),Runners), member(runner(_,H1,F7,providence,_),Runners), member(runner(_,_,F8,williamsburg,I2),Runners), member(runner(_,_,G,_,blue),Runners), member(runner(larry,_,_,J1,J2),Runners), member(runner(_,gould,K1,_,_),Runners), member(runner(_,_,K2,_,green),Runners), member(runner(_,_,_,_,purple),Runners), member(runner(_,_,_,_,red),Runners), member(runner(_,_,_,_,yellow),Runners), member(runner(_,kendrig,_,_,_),Runners), member(runner(_,whitley,_,_,_),Runners), (A1 = 155 | A1 = 160), (F2 = 155 | F2 = 160), A1 \= F2, B \= purple, (D1 = 165 | D1 = 170), D2 < D1, F4 = 150, F4 < F5, F4 < F6, F4 < F7, F4 < F8, F2 < F3, G = 155, H1 \= kendrig, F7 \= 165, (I1 = blue | I1 = purple), (I2 = blue | I2 = purple), I1 \= I2, J1 \= northhampton, J2 \= green, K2 >= 165, K1 >= 165, F6 \= 160. /** * 9.29 */ % house#, color, nationality, pet, drink, cloth % house numbers houses([house(1,_,_,_,_,_), house(2,_,_,_,_,_), house(3,_,_,_,_,_), house(4,_,_,_,_,_), house(5,_,_,_,_,_)]). /* % house colors houses([house(_,red,_,_,_,_), house(_,green,_,_,_,_), house(_,ivory,_,_,_,_), house(_,yellow,_,_,_,_), house(_,blue,_,_,_,_)]). % nationalities houses([house(_,_,finn,_,_,_), house(_,_,spaniard,_,_,_), house(_,_,ukranian,_,_,_), house(_,_,norwegian,_,_,_), house(_,_,japanese,_,_,_)]). % pets houses([house(_,_,_,dog,_,_), house(_,_,_,snails,_,_), house(_,_,_,horse,_,_), house(_,_,_,fox,_,_), house(_,_,_,zebra,_,_)]). % drinks houses([house(_,_,_,_,coffee,_), house(_,_,_,_,tea,_), house(_,_,_,_,milk,_), house(_,_,_,_,orangejuice,_), house(_,_,_,_,vodka,_)]). % clothes houses([house(_,_,_,_,_,skirt), house(_,_,_,_,_,jeans), house(_,_,_,_,_,slacks), house(_,_,_,_,_,woolies), house(_,_,_,_,_,vest)]). */ % house#, color, nationality, pet, drink, cloth zebra( Houses ) :- houses( Houses ), member(house(_,red,finn,_,_,_),Houses), member(house(_,_,spaniard,dog,_,_),Houses), member(house(C1,green,_,_,coffee,_),Houses), member(house(C2,ivory,_,_,_,_),Houses), member(house(_,_,ukranian,_,tea,_),Houses), member(house(_,_,_,snails,_,skirt),Houses), member(house(F1,yellow,_,_,_,jeans),Houses), member(house(F2,_,_,horse,_,_),Houses), member(house(3,_,_,_,milk,_),Houses), member(house(H1,_,norwegian,_,_,_),Houses), member(house(H2,blue,_,_,_,_),Houses), member(house(I1,_,_,_,_,slacks),Houses), member(house(I2,_,_,fox,_,_),Houses), member(house(_,_,_,_,orangejuice,woolies),Houses), member(house(_,_,japanese,_,_,vest),Houses), member(house(_,_,_,_,vodka,_),Houses), member(house(_,_,_,zebra,_,_),Houses), adjacent(C1,C2), adjacent(F1,F2), H1 = 1, adjacent(H1,H2), adjacent(I1,I2).