0
votes

I have two questions I am working on and I'm simply having a hard time understanding the question. The first being

Create a Prolog predicate flatten_append/3 which has 3 arguments that are all lists. The third list should be equivalent to the concatenation of the flattened versions of the first list followed by the flattened version of the second list. For example, flatten append([1,2,[3,4,5],[6]],[[7],[8,[9]]],X) should succeed binding X to the list [1,2,3,4,5,6,7,8,9]. Use the built-in predicates append/3 and flatten/2.

Does creating a predicate mean I make a new file that I then consult to run? Also for three arguments is that like this thing([], [X], X) or would that only be one and I need something like this for three arguments?

thing([],[X],X).

thing([H|T],[H|X],Y) :- H = 1, anything(Y,Z), thing(T,X,Z).

thing([H|T],[H|X],Y) :- H = 0, nothing(Y,Z), thing(T,X,Z).

Finally what does it mean when it says succeed binding X? Is there a specific order or at the end you assign X equal to what you just made? Thanks

1

1 Answers

0
votes

Does creating a predicate mean I make a new file that I then consult to run?

It actually means "add a predicate to the Prolog database" so that can be run from REPL (Read-Eval-Print Loop, traditionally called the "Prolog toplevel"). But yes, this is done by creating a file my_predicate.pl and then typing consult(my_predicate). or [my_predicate]. (also, make. to reload it after a change) at the REPL. Note the "dot" at the end to terminate the command.

for three arguments is that like this thing([], [X], X)

Yes exactly. This is predicate thing/3 which relates the three arguments somehow. How it does so what is the preferred direction of information flow (e.g. in thing(X,Y,Z), you may compute Z from X and Y, or verify Z given X and Y or even compute X and Y given `Z) depends on the implementation.

The implementation of thing/3 that you give consists of 3 clauses. 1 is a "fact" (has only a head), 2 are "rules" (have a head and a body).

what does it mean when it says succeed binding X?

It means that the computation successfully computes a value, makes X equal to it and returns successfully. As in flatten append([1,2,[3,4,5],[6]],[[7],8,[9]]],X) "succeeds" and on return X "is" (contains/is bound to) the flattened list. X behaves like a reference to a global variable.

Take some time to read the intro: http://www.learnprolognow.org/ and exercise yourself in https://swish.swi-prolog.org/