Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of Append in prolog.

Status
Not open for further replies.

cyrow

Programmer
Apr 16, 2009
1
US
I am having a difficult time with this question and need some help. Below is my answer, I am not sure if they are correct, but based on my limited understanding. Need some help.

recursive definition of append

append([]A,A).
append([H|T1], A, [H|T2]):-append(T1, A, T2)

(a.) ?-append(L,[x,z],[y,x,z]

my answer
L3 = [L]|[Lo]
?- append ([], [x,y], L0.
L0 = []|L1

(b.) write rule for append1(l1, l2, l3,l4) to be true if and only if l4 is the same as l1,l2, and l3 append in that order

my answer
append1([], A, A, A).
append1([H|T1], A, A, [H|T4]):-append1(T1, A, A, T4)

(c.) using append write rule to define shorten(l1, l2) to be true if and only if l2 can be obtained by removing the first and last elements of l1.

shorten(L1, L2) :- shorten([H]|[_N], L2)

(d.) use shorten write rule that defines even(l) to be true if and only if l is a list of even number
shorten(l1,l2):-even(l,X) //where x is 2


(e.) state steps prolog would take to solve shorten([1,2,3],l)

[1]|[,2,3]
[2][3]
[3]
 
Code:
append([]A,A).
append([H|T1], A, [H|T2]):-append(T1, A, T2)
looks good.

(a.) ?-append(L,[x,z],[y,x,z]
I think that you should say what is L.

?- append ([], [x,y], L0).
Idem for that.

(b.) write rule for append1(l1, l2, l3,l4) to be true if and only if l4 is the same as l1,l2, and l3 append in that order
Why don't you use "append" for this question ?

(c.) using append write rule to define shorten(l1, l2) to be true if and only if l2 can be obtained by removing the first and last elements of l1.
Are-you sure that you must use append and not append1 to solve this question ? It's easier with append1.

(d.) use shorten write rule that defines even(l) to be true if and only if l is a list of even number
Think of the use of shorten on a list.
What does append if you use shorten recursively on that list ?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top