0% found this document useful (0 votes)
9 views1 page

Prolog Predicate Examples and Queries

The document provides Prolog predicates for finding the last element of a list and removing the last element from a list. It also includes a series of queries with their corresponding results, indicating whether they are true or false, along with substitutions when applicable. The examples illustrate the functionality of the predicates and the outcomes of various queries.

Uploaded by

G Y
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Prolog Predicate Examples and Queries

The document provides Prolog predicates for finding the last element of a list and removing the last element from a list. It also includes a series of queries with their corresponding results, indicating whether they are true or false, along with substitutions when applicable. The examples illustrate the functionality of the predicates and the outcomes of various queries.

Uploaded by

G Y
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Ex. 1: Write each of these predicates in Prolog by filling in the blanks.

a) [5 pts] last(L, X) is satisfied when X is the final element of list L.


Example: last([2,3,4,5,6,7], X) yields X = 7.

last([X], X).
last([ _|T], X) :- last(T, X).

b) [5 pts] removelast(L, L2) is satisfied when L2 is formed from list L by discarding the final
element of L. Example: removelast([2,3,4,5,6,7], Y) yields Y = [2,3,4,5,6].

removelast([ _ ], [ ]).
removelast([H|T], [H|T2]) :- removelast(T, T2).

Ex.2: Consider the following predicates:

For each query, state whether the query is true or list all of the substitution(s) that make the
query true. If the query is false or if there are no substitutions, write false .
a) ?- plays(patrick,X).
X = mayo;
X= drums.

b) ?- twoPlayers(X).
X=drums;
X=drums.

c) ?- talented(squidward).
False.

d) ?- band1([patrick, flagTwirlers], X).


False.

e) ?- band1(X, [drums, mayo]).


X=[patrick,patrick]

Common questions

Powered by AI

Prolog uses a logical structure based on declarative programming and recursion for defining and resolving predicates. The 'last' predicate is defined using base and recursive cases, reducing the problem size iteratively. Similarly, 'removelast' utilizes pattern matching to differentiate cases, recursively constructing a new list by trimming elements until the last is omitted. This structure emphasizes logic over explicit flow control, leveraging recursion for processing lists .

The query `?- talented(squidward)` evaluates to false because, according to the provided predicates, there is no definition or fact within the Prolog knowledge base that associates 'talented' with 'squidward'. Thus, Prolog cannot find a substitution or deduction to satisfy the predicate .

To ensure 'last' works only with lists containing at least one element, the predicate definition can assert a precondition checking for non-empty lists: 1) `last([X|[]], X).` This ensures the base case works only when the list reduces to exactly one element and a transition to `[]` is not possible. 2) `last([ _|T], X) :- T \= [], last(T, X).` Including a condition like `T \= []` prevents the recursive rule from applying to empty lists, ensuring at least one element remains .

Recursion is essential in predicates like 'last' and 'removelast' because it allows the handling of arbitrary-length lists by systematically breaking down the problem. Recursion in 'last' enables progression through the list to identify the terminal element, and in 'removelast', it facilitates constructing the list minus the last element. This progressive breakdown into simpler subproblems aligns with Prolog's systematic resolution principle, contributing significantly to their functionality .

Placing base cases incorrectly in recursive predicates like 'removelast' can cause infinite recursion or failure to terminate. For instance, if the base case `removelast([ _ ], [ ]).` is not positioned prior to recursive calls, recursive cases might continue despite reaching intended termination conditions. This can be avoided by ensuring base cases are defined first, stopping recursion before additional calls proceed .

The 'removelast(L, L2)' predicate functions to create list 'L2' by removing the last element from list 'L'. It is implemented in Prolog with two rules: 1) `removelast([ _ ], [ ]).` This base case handles lists with a single element, resulting in an empty list. 2) `removelast([H|T], [H|T2]) :- removelast(T, T2).` This recursive rule builds list 'L2' by maintaining the head of the list while processing the tail to remove the last element .

The query `?- band1([patrick, flagTwirlers], X)` returns false because there is no matching predicate in the provided data for 'band1' that would allow a substitution for 'X' that makes the query true. The tuple '[patrick, flagTwirlers]' does not comply with any existing pattern in the known facts or rules for 'band1' .

The query `?- band1(X, [drums, mayo])` results in `X=[patrick,patrick]` because the Prolog predicate is designed such that for the list '[drums, mayo]', it associates those instruments with 'patrick'. To satisfy this condition, it requires that 'patrick' be patterned in such a way as to match both instruments, thus resulting in 'X' being unified with `[patrick,patrick]` .

The query `?- plays(patrick,X)` in the given Prolog predicates yields the results `X = mayo; X = drums.` This means that Patrick plays both 'mayo' and 'drums', as the query evaluates to a valid Prolog substitution for these two values of 'X' .

The predicate 'last(L, X)' is satisfied when 'X' is the final element of list 'L'. It is defined in Prolog with two facts/rules: 1) `last([X], X).` This states that if a list consists of a single element 'X', then 'X' is the last element. 2) `last([ _|T], X) :- last(T, X).` This recursive rule states that 'X' is the last element of the list 'T', if 'X' is the last element of the tail of 'L' .

You might also like