%  HOMEWORK  8

 

/*###############################################################################

#################################################################################

QUESTION 1

Adding functionality to last week homework

#################################################################################

###############################################################################*/

 

%-------- From Last week ---------------

val1(0,0).

val1(s(0),1):-!.

val1(s(A),B) :-  val1(A,C), B is C+1,!.

val1(s(A),B) :-  val1(A,C), B is C+1,nonvar(B).

 

val2(0,0).

val2(s(0),1).

val2(s(A),B) :-  val2(A,C), B is C+1,nonvar(s(A)) .

 

value(A,B):-nonvar(A),nonvar(B),fail.

value(A,B):-nonvar(A),val1(A,B),!.

value(A,B):-nonvar(B),val2(A,B),!.

 

addS(0,0,0):-!.

addS(0,B,B):-!.

addS(A,0,A):-!.

addS(A,B,S):-value(A,X), value(B,Y), Stemp is X+Y, value(S,Stemp).

 

subS(0,0,0):-!.

subS(0,B,0):-!.

subS(A,0,A):-!.

subS(A,B,S):-value(A,X), value(B,Y), X>Y,Stmp is X-Y, value(S,Stmp).

subS(A,B,0):-value(A,X), value(B,Y), X=<Y.

 

mulS(0,B,0):-!.

mulS(A,0,0):-!.

mulS(0,0,0):-!.

mulS(A,B,S):-value(A,X), value(B,Y), Stemp is X*Y, value(S,Stemp).

%---------------------------------------

 

valueA(N1+N2,S1+S2):-value(S1,N1),value(S2,N2).

valueA(N1-N2,S1-S2):-value(S1,N1),value(S2,N2).

valueA(N1*N2,S1*S2):-value(S1,N1),value(S2,N2).

 

sArithm(S1+S2,F):-addS(S1,S2,F).

sArithm(S1-S2,F):-subS(S1,S2,F).

sArithm(S1*S2,F):-mulS(S1,S2,F).

 

/*###############################################################################

QUESION 2 choose 3 puzzles==>

#################################################################################

US States Logic Puzzle (California, Texas, Illinois, New York )

---------------------------------------------------------------

Four children live in different US states. Find out where each of them lives.

  1. Bill's state does not have an "x" in it; neither does Ann's.

  2. Mark's state is a single word.

  3. Bill does not live near the west coast of the US.

  4. Ann lives west of the Mississippi River.

  5. Mark's state borders a Great Lake.

  6. Jon live in a state different than Ann,Bill,Mark.

#################################################################################

###############################################################################*/

 

/*====================================================================

permute([],[]).

permute(X,Y) :- var(X), permute(Y,X).

permute(L1, [L2H | L2T]) :-not(var(L1)),

                           append(L1F1, [L2H | L1F2], L1),

                           append(L1F1, L1F2, L1S),

permute(L1S, L2T).

 

permute(X,[a,b]),permute(Y,[x,y])==>

X = [a, b],  Y = [x, y] ;

X = [a, b],  Y = [y, x] ;

X = [b, a],  Y = [x, y] ;

X = [b, a],  Y = [y, x] ;         

 

member(X,[a,b]),member(Y,[x,y]).

X = a,  Y = x ;

X = a,  Y = y ;

X = b,  Y = x ;

X = b,  Y = y ;

 

Instead of using "permute" , I used "member" to generate combinations.

====================================================================*/

 

no_x_state(california).

no_x_state(illinois).

no_x_state(newyork).

single_word_state(california).

single_word_state(texas).

single_word_state(illinois).

west_coast_state(california).

west_coast_state(illinois).

west_missipi_river(california).

west_missipi_river(texas).

near_great_lake(illinois).

 

ann_state(P,S):-  P=ann,no_x_state(S),west_missipi_river(S).

 

bill_state(P,S):- P=bill,no_x_state(S),not( west_coast_state(S)).

 

mark_state(P,S):- P=mark,single_word_state(S), near_great_lake(S).

 

jon_state(P,S):-  P=jon,ann_state(ann,B),bill_state(bill,C),mark_state(mark,D),

                            not(S=B),not(S=C),not(S=D),!.

 

person_state(X):-Kids = [ann,bill,mark,jon],States=[california,illinois,newyork,texas],

                 member(P1,Kids),member(P2,Kids),member(P3,Kids),member(P4,Kids),

                 member(S1,States),member(S2,States),member(S3,States),member(S4,States),       

                 ann_state(P1,S1),bill_state(P2,S2),mark_state(P3,S3),jon_state(P4,S4),

                 X=[[P1,S1],[P2,S2],[P3,S3],[P4,S4]].

 

/*======================================================

RESULT:

person_state(X)==>

X = [[ann, california], [bill, newyork], [mark, illinois], [jon, texas]] ;

 

<ann, california >, no "x" in the state name, west of missipi

<bill, newyork >  , no "x" in the state name, not west of missipi

<mark, illinois > , single word state name, near great lake

<jon, texas>

======================================================*/

 

/*###############################################################################

#################################################################################

Favorite Dinausaurs of the kids

-------------------------------

Dinosaurs Logic Puzzle

Dinosaurs=(apatosaurus,pachycephalosaurus,spinosaurus,utahraptor)

Kids=(Al,Barb,Cyd,Don)

 

1. Al's favorite dinosaur is a meat-eater.

2. Barb's favorite dinosaur does not have a sail on its back;

   neither does Cyd's.

3. Cyd's favorite dinosaur was not named after a US state.

4. Cyd's favorite dinosaur does not have an extremely long neck.

5. Al's favorite dinosaur ends in an "s."

6. Barb's favorite dinosaur does not start with an "A."

7. Don like a dinosaur different from Al,Barb,Cyd.

 

#################################################################################

###############################################################################*/

 

meat_eater(spinosaurus).

meat_eater(utahraptor).

 

no_back_sail(apatosaurus).

no_back_sail(pachycephalosaurus).

no_back_sail(utahraptor).

 

no_us_state(apatosaurus).

no_us_state(pachycephalosaurus).

no_us_state(spinosaurus).

 

no_long_neck(pachycephalosaurus).

no_long_neck(spinosaurus).

no_long_neck(utahraptor).

 

end_with_s(apatosaurus).

end_with_s(pachycephalosaurus).

end_with_s(spinosaurus).

 

no_start_a(pachycephalosaurus).

no_start_a(spinosaurus).

no_start_a(utahraptor).

 

al_dino(P,D)  :-P=al, meat_eater(D),end_with_s(D).

 

barb_dino(P,D):-P=barb, no_back_sail(D), no_start_a(D).

 

cyd_dino(P,D) :-P=cyd, no_back_sail(D), no_us_state(D),no_long_neck(D).

 

don_dino(P,D) :-P=don, al_dino(al,A),barb_dino(barb,B),cyd_dino(cyd,C),

                not(D=A),not(D=B),not(D=C),!.

               

kid_dino(X):-Kids = [al,barb,cyd,don],

                     Dinos= [apatosaurus,pachycephalosaurus,spinosaurus,utahraptor],

                     member(K1,Kids),member(K2,Kids),member(K3,Kids),member(K4,Kids),

                     member(D1,Dinos),member(D2,Dinos),member(D3,Dinos),member(D4,Dinos),

                     al_dino(K1,D1),barb_dino(K2,D2),cyd_dino(K3,D3),don_dino(K4,D4),

                     not(D1=D2),not(D1=D3),not(D1=D4),not(D2=D4),

                     not(D2=D3),not(D2=D4),not(D3=D4),               

                     X=[[K1,D1],[K2,D2],[K3,D3],[K4,D4]].             

 

/*======================================================

RESULT:

kid_dino(X)==>

X = [[al, spinosaurus], [barb, utahraptor], [cyd, pachycephalosaurus], [don, apatosaurus]] ;

 

<al   = spinosaurus>        , is a meat eater and end with and "s"

<barb = utahraptor>         , have no back sail and does'nt start with an "a"

<cyd  = pachycephalosaurus> , no back sail, not named after a us state and no long neck.

<don  = apatosaurus>

======================================================*/

 

/*###############################################################################

#################################################################################

Last Names Logic Puzzle

------------------------

LastNames=(smith, jones, gonzales, lee )

FirstNames=(sam, lil, dan, barb )

         

1. Barb's last name does not have a "z" in it.

2. Sam's last name does not end with an "s".

3. Lil's last name has 5 letters in it.

4. Sam's last name is not the shortest one here.

5  Dan has a different last name than sam,lil,barb.

#################################################################################         

###############################################################################*/

 

no_z(smith).

no_z(jones).

no_z(lee).

no_end_s(smith).

no_end_s(lee).

five_letter(smith).

five_letter(jones).

not_shortest(smith).

not_shortest(jones).

not_shortest(gonzales).

 

sam_lname(F,L):-  F=sam, no_end_s(L),not_shortest(L).

 

lil_lname(F,L):-  F=lil, five_letter(L).

 

barb_lname(F,L):- F=barb, no_z(L).

 

dan_lname(F,L):-  F=dan, sam_lname(sam,A),lil_lname(lil,B),barb_lname(barb,C),

                  not(L=A),not(L=B),not(L=C),!.

               

last_name(X):-Fnames = [sam,lil,barb,dan],

                         Lnames = [smith,jones,gonzales,lee],

                         member(F1,Fnames),member(F2,Fnames),member(F3,Fnames),member(F4,Fnames),

                         member(L1,Lnames),member(L2,Lnames),member(L3,Lnames),member(L4,Lnames),             

                         sam_lname(F1,L1),lil_lname(F2,L2),barb_lname(F3,L3),dan_lname(F4,L4),             

                         not(L1=L2),not(L1=L3),not(L1=L4),not(L2=L4),

                         not(L2=L3),not(L2=L4),not(L3=L4),             

                         X=[[F1,L1],[F2,L2],[F3,L3],[F4,L4]].             

              

/*======================================================

RESULT:

last_name(X)==>

X = [[sam, smith], [lil, jones], [barb, lee], [dan, gonzales]] ;

 

<sam  = smith>    , not ending with "s", not the shortest last name

<lil  = jones>        , five letters

<barb = lee>        , no "z" in last name

<dan  = gonzales>

======================================================*/

 

/*#############################################################################

###############################################################################

###############################################################################*/