Andrew Price - Prolog 1

  • Substance(
  • Web log,
  • Projects ).
  • Geekdom

    Logo

    My First Prolog Coursework

    My first Prolog coursework in which I totally failed to realise that /* */ is a block comment and still got 100% in, much to my surprise.

    Here are the questions and official answers in pdf format (Courtesy of Dr. Beckmann)

    Begin prolog1.pro:

    % The possible suspects of the crimes are:
    
    possible_suspect(fred).
    possible_suspect(mary).
    possible_suspect(jane).
    possible_suspect(george).
    
    % The facts about the crimes from the police log.
    
    crime(robbery1, john, tuesday, park).
    crime(assault1, mary, wednesday, park).
    crime(robbery2, jim, wednesday, pub).
    crime(assault2, robin, thursday, park).
    
    % Tell prolog where the suspects were on
    % different days.
    
    was_at(fred, park, tuesday).
    was_at(fred, pub, wednesday).
    was_at(fred, pub, thursday).
    
    was_at(george, pub, tuesday).
    was_at(george, pub, wednesday).
    was_at(george, home, thursday).
    
    was_at(jane, home, tuesday).
    was_at(jane, park, wednesday).
    was_at(jane, park, thursday).
    
    was_at(mary, pub, tuesday).
    was_at(mary, park, wednesday).
    was_at(mary, home, thursday).
    
    % Tell prolog who is jealous of who
    
    jealous_of(fred, john).
    jealous_of(jane, mary).
    
    % And who owes money to whom
    
    owes_money_to(george, jim).
    owes_money_to(mary, robin).
    
    % A Person has a motive against a Victim if
    % Person is jealous of Victim or
    % Person owes money to Victim
    
    motive_against(Person, Victim) :-
        jealous_of(Person, Victim);
        owes_money_to(Person, Victim).
    
    % A Person is a prime suspect of a crime if
    % Person is a possible suspect and the person
    % was at the time and place of the crime and
    % the person had a motive against the victim
    % of the crime.
    
    prime_suspect(Person, Crime) :-
        possible_suspect(Person),
        was_at(Person, Place, Day),
        crime(Crime, Victim, Day, Place),
        motive_against(Person, Victim).
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % QUERIES / RESPONSES FOR Q1
    %
    % 1 ?- prime_suspect(fred,robbery1).
    % 
    % Yes
    % 2 ?- prime_suspect(george, assault2).
    % 
    % No
    % 3 ?- prime_suspect(Prime_Suspect, robbery1).
    % 
    % Prime_Suspect = fred ;
    % 
    % No
    % 4 ?- prime_suspect(Prime_Suspect, robbery2).
    % 
    % Prime_Suspect = george ;
    % 
    % No
    % 5 ?- prime_suspect(Prime_Suspect, assault1).
    % 
    % Prime_Suspect = jane ;
    % 
    % No
    % 6 ?- prime_suspect(Prime_Suspect, assault2).
    % 
    % No
    % 7 ?- prime_suspect(fred, Crime).
    % 
    % Crime = robbery1 ;
    % 
    % No
    % 8 ?- prime_suspect(george, Crime).
    % 
    % Crime = robbery2 ;
    % 
    % No
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %     Q2. MISC                                  %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % 1. Compute the last member of a nonempty list
    
    last([X],X).
    last([_|T],X) :-
        last(T,X).
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % 2. Reverse a list
    
    append([],L,L).
    append([H|T],L2,[H|L3]) :-
        append(T,L2,L3).
    
    reverse([],[]).
    reverse([H|T],R) :-
        reverse(T,X),
        append(X,[H],R).
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % 3. Remove one occurrence of X from list K
    
    select(X,K,L) :-
        append(I,[X|J],K),
        append(I,J,L).
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % 4. X and Y are different members of L
    
    member(X,[X|_]).
    member(X,[_|Yt]) :-
        member(X,Yt).
    
    % X and Y are different members of L if
    % X is a member of L and Y is a member of
    % list K where K is list L with X removed.
    
    different(X,Y,L) :-
        member(X,L),
        select(X,L,K),
        member(Y,K).
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % 5. K and L are lists of the same length if
    %    they are both empty after chopping their
    %    heads off the same amount of times.
    
    samelength([],[]).
    samelength([_|K],[_|L]) :-
        samelength(K,L).
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % 6. K and L are lists of the same length and
    %    contain the same elements.
    
    same_elements([],[]).
    same_elements([Kh|Kt],L) :-
        select(Kh,L,X),
        same_elements(Kt,X).
    
    % List K is similar to list L if
    % K and L are the same length and
    % K and L contain the same elements
    
    similar([],[]).
    similar(K, L) :-
        samelength(K,L),
        same_elements(K,L).
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % QUERIES / RESPONSES FOR Q2
    %
    % 2 ?- last([a,b,c,d],Last).
    %
    % Last = d ;%
    %
    % No
    % 3 ?- reverse([a,b,c,d],Reversed).
    %
    % Reversed = [d, c, b, a] ;
    %
    % No
    % 4 ?- select(a,[a,b,c,a],List).
    %
    % List = [b, c, a] ;
    %
    % List = [a, b, c] ;
    %
    % No
    % 5 ?- different(X,Y,[1,2,3]).
    %
    % X = 1
    % Y = 2 ;
    %
    % X = 1
    % Y = 3 ;
    %
    % X = 2
    % Y = 1 ;
    %
    % X = 2
    % Y = 3 ;
    %
    % X = 3
    % Y = 1 ;
    %
    % X = 3
    % Y = 2 ;
    %
    % No
    % 6 ?- samelength([a,b,c],L).
    %
    % L = [_G359, _G362, _G365] ;
    %
    % No
    % 7 ?- samelength(L,[a,b,c]).
    %
    % L = [_G359, _G362, _G365] ;
    %
    % No
    % 8 ?- similar([a,b,a],L).
    %
    % L = [a, b, a] ;
    %
    % L = [a, a, b] ;
    %
    % L = [b, a, a] ;
    %
    % L = [a, a, b] ;
    %
    % L = [b, a, a] ;
    %
    % L = [a, b, a] ;
    %
    % No
    % 9 ?- similar(L,[a,b,a]).
    %
    % L = [a, b, a] ;
    %
    % L = [a, a, b] ;
    %
    % L = [b, a, a] ;
    %
    % L = [b, a, a] ;
    %
    % L = [a, a, b] ;
    %
    % L = [a, b, a] ;
    %
    % No
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    

    [ Written in Vim | Validate my: XHTML / CSS ]