Accountancy, asked by sudeeptaranibeero, 6 months ago

5)
a.
Fill in the blanks.
x 2 = -96
b. -18 x
= -270
C.
x 20 = -140
d. 20 x (-2) x 5 x
= -1400
llont into or so that 5 x 12 x n is the product of three consecutive integers.
]​

Answers

Answered by Anonymous
2

Answer:

Prolog uses brackets [...] as a list builder. The notation [X|Y] refers to a list whose first element is X and whose tail is Y. A finite list can be explicitly enumerated, such as [1,2,3,4]. The following three definitions should make sense to a Lisp programmer, where 'car' refers to the first element of a list, 'cdr' refers to the tail or rest of the list, and 'cons' is the list constructor.

car([X|Y],X).

cdr([X|Y],Y).

cons(X,R,[X|R]).

meaning ...

The head (car) of [X|Y] is X.

The tail (cdr) of [X|Y] is Y.

Putting X at the head and Y as the tail constructs (cons) the list [X|R].

However, we will see that these explicit definitions are unneeded. A list whose head is X and whose tail is Y can just be referred to using the Prolog term [X|Y]. Conversely, if the list can be unified with the Prolog term '[X|Y]' then the first element of the list is bound to (unified with) X and the tail of the list is bound to Y.

Many of the predicates discussed in this section are "built-in" for many Prolog interpreters.

Consider the following definition of the predicate 'member/2'.

member(X,[X|R]).

member(X,[Y|R]) :- member(X,R).

One can read the clauses the following way, respectively:

X is a member of a list whose first element is X.

X is a member of a list whose tail is R if X is a member of R.

This program can be used in numerous ways. One can test membership:

?- member(2,[1,2,3]).

Yes

One can generate members of a list:

?- member(X,[1,2,3]).

X = 1 ;

X = 2 ;

X = 3 ;

No

Here is a derivation tree showing how this last goal generated all of the answers.

Fig. 2.7

Fig. 2.7

Each left branch corresponds to a match (unification) against the first clause for 'member' and each right branch corresponds to a match against the second clause. The subgoal 'member(X,[])' on the lowest right branch will not match the head of any 'member' clause. In particular '[]' will not unify with a pattern of the form '[X|R]' because the latter represents a list with at least one element.

We will find many other uses for 'member'. This example query ...

?- member([3,Y], [[1,a],[2,m],[3,z],[4,v],[3,p]]).

Y = z ;

Y = p ;

No

... suggests a use where one intends to search in order to find elements paired with a specified element. Here is another, finding elements of a list which satisfy some constraint:

?- member(X,[23,45,67,12,222,19,9,6]), Y is X*X, Y < 100.

X = 9 Y = 81 ;

X = 6 Y = 36 ;

No

The definition for 'member' is usually written

member(X,[X|_]).

member(X,[_|R]) :- member(X,R).

where '_' (underscore) designates a "don't-care" variable, usually called anonymous variables. In general, such variables have names whose first character is the underscore. In effect, they match any Prolog term, but no variable binding results from the free match. Notice that this is consistent with the original intentions of the definition of 'member'. Not having to bind values to anonymous variables saves a little run-space and run-time.

Related to 'member' is the following definition for 'takeout'.

takeout(X,[X|R],R).

takeout(X,[F|R],[F|S]) :- takeout(X,R,S).

These clauses can be paraphrased in English as follows:

When X is taken out of [X|R], R results.

When X is taken out of the tail of [X|R], [X|S] results, where S is the result of taking X out of R.

For example,

?- takeout(X,[1,2,3],L).

X=1 L=[2,3] ;

X=2 L=[1,3] ;

X=3 L=[1,2] ;

No

Notice that it would not be appropriate to use any anonymous variables in the definition of 'takeout'. Here is a program clause tree showing that 'takeout(3,[1,2,3],[1,2])' is a consequence of the definition. Pay particular attention to exactly how the clauses are used to construct the tree.

takeout(3,[1,2,3],[1,2])

|

|

takeout(3,[2,3],[2])

|

|

takeout(3,[3],[])

|

|

true

The following goal,

?- takeout(3,W,[a,b,c]).

W = [3,a,b,c] ;

W = [a,3,b,c] ;

W = [a,b,3,c] ;

W = [a,b,c,3] ;

No

shows that 'takeout(X,Z,W)' can also be interpreted as "insert X into W to produce Z". That is, 'takeout' got its name from just one of its uses. Of course, one could define

putin(X,L,R) :- takeout(X,R,L).

Here is a definition for appending, or concatenating, two Prolog lists.

append([X|Y],Z,[X|W]) :- append(Y,Z,W).

append([],X,X).

Several kinds of goals are possible

Similar questions