# Simple Prolog

PROLOG

Due 7/10

NOTE: For your homework, you are not allowed to use the builtin predicate

definitions. If, for example, you want to use member/2, create you own

version called mymember/2 (or something). Same for append, reverse,

sort, and many others.You may use any builtin numeric operators, 'is', the '[' ']'

brackets for lists, fail, not, and maybe some others

â€¢ (5) In Prolog, implement the predicate cube/2. This predicate will relate a list of numbers to a list of numbers that, for each number in the first list, contains the number, the square of the number, and the cube of the number. The only modes for this predicate are: cube(+,-), and cube(+,+). E.g.,

?- cube([2,1,4],A).

A = [2,4,8,1,1,1,4,16,64]

?- cube([],[]).

yes.

?- cube([5],[5,25,125]).

yes.

?- cube([2,3],[2,3,4]).

no.

â€¢ (5) In Prolog, implement the predicate dotProduct/3. This predicate will relate two vectors (represented as lists) to their dot product. The only modes for this predicate are: dotProduct(+,+,-), and dotProduct(+,+,+). E.g.,

?- dotProduct([1,2,3],[5,5,5],A).

A = 30

?- dotProduct([2],[12],24).

yes.

?- dotProduct([],[0],0).

yes.

?- dotProduct([2,3],[2,3,4],17).

no.

â€¢ (45) In Prolog, create an ADT for Sets. Each Set will be represented as a list of unique atoms. Assume the same semantics as in Hw3.

o (5) setAddElt(E,S1,S2).

ï‚§ S2 = S1 Union {E}

ï‚§ Assume modes: setAddElt(+,+,-), and SetAddElt(+,+,+).

o (5) setDelElt(E,S1,S2).

ï‚§ S2 = S1 - {E}

ï‚§ Assume modes: setDelElt(+,+,-), and SetDelElt(+,+,+).

o (5) setMember(E,S).

ï‚§ return E element-of S

ï‚§ Assume any mode.

o (10) setEqual(S1,S2).

ï‚§ return S1 == S2 (in Prolog)

ï‚§ Assume modes: setEqual(+,+).

o (10) SetUnion(S1,S2,S3).

ï‚§ S3 = S1 Union S2

ï‚§ Assume modes: setUnion(+,+,-), and SetUnion(+,+,+).

o (10) Set Intersect(S1,S2,S3).

ï‚§ S3 = S1 Intersection S2

ï‚§ Assume modes: setIntersect(+,+,-), and SetIntersect(+,+,+).

â€¢ (20) Because Prolog's search engine has the ability to backtrack to find a solution, Prolog can easily be used to implement a top-down parser.

o Assume the following Context-Free-Grammar (CFG) for Boolean Expressions, with start symbol E.

ï‚§ E --> A or E | A

ï‚§ A --> B and A | B

ï‚§ B --> not B | C

ï‚§ C --> '(' E ')' | true | false

o Using Prolog, construct a top-down parser that can be used to parse and evaluate arbitray Boolean expressions. e.g.,

?- parse(['(',true,and,false,')',or true],V).

V = true.

?- parse([true,or,true,and,false],V).

V = true.

?- parse([true,or,or,true,or,false],V).

No.

o Hint: Create a different predicate for each nonterminal in the grammar.

â€¢ (25) Assume, you only have the numbers 1, 2, 3, and 4. Further assume that you only have the operators '+' and '*'. Lastly, assume that you can only use a number once.

o Using Prolog, determine the number of arithmetic expressions that can be create to equate to the numbers 30, 31, 32, 33, 34, and 35.

ï‚§ For example, there are 44 equations that equal 20, e.g.,

?- findExpr(20,E).

E = [1, *, [4, *, [2, +, 3]]] ;

E = [1, *, [4, *, [3, +, 2]]] ;

E = [1, *, [[2, +, 3], *, 4]] ;

E = [1, *, [[3, +, 2], *, 4]] ;

E = [4, *, [2, +, 3]] ;

E = [4, *, [2, +, [1, *, 3]]] ;

E = [4, *, [2, +, [3, *, 1]]] ;

E = [4, *, [3, +, 2]] ;

...

â€¢ Hint: Use the following as a starting place.

findExpr(N,E):-

generateExpr(...),

computeValue(...).

https://brainmass.com/math/basic-algebra/simple-prolog-5819

#### Solution Preview

I hope this will be enough for you to get a good mark, if not 100%.

PROLOG

Due 7/10

NOTE: For your homework, you are not allowed to use the builtin predicate

definitions. If, for example, you want to use member/2, create you own

version called mymember/2 (or something). Same for append, reverse,

sort, and many others.You may use any builtin numeric operators, 'is', the '[' ']'

brackets for lists, fail, not, and maybe some others

â€¢ (5) In Prolog, implement the predicate cube/2. This predicate will relate a list of numbers to a list of numbers that, for each number in the first list, contains the number, the square of the number, and the cube of the number. The only modes for this predicate are: cube(+,-), and cube(+,+). E.g.,

?- cube([2,1,4],A).

A = [2,4,8,1,1,1,4,16,64]

?- cube([],[]).

yes.

?- cube([5],[5,25,125]).

yes.

?- cube([2,3],[2,3,4]).

no.

Hi, I'm not going to cheat you at this point. I'm not an expert of PROLOG. Although I've learned the class and got a good marks, I don't think I can 100% solve your problem. Also, I cannot compile and run PORLOG now.

I reply this posting only because I found that there is nobody reply it for one full day and the deadline is near. The program maynot work on the computer but I believe that the principle is Correct.

For this question the idea is ...

#### Solution Summary

Simple Prolog is assessed. A different predicted for nonterminal in the grammar are created.