user1
user1

Reputation: 37

Adding element to beginning of list, without creating a new list

I want to add [Startline, Assumption, assumption] to the beginning of list CheckedProof (without creating a new list). Why doesn't this first code snippet give the same result for CheckedProof as the one where I just use append? Is it possible to yield the same result by just using append?

updateList([Element|List1], Element).
updateList(List1, Element):- !,
updateList([Element|List1], Element),!.

updateList(CheckedProof, [Startline, Assumption, assumption]).
append(CheckedProof, [], TemporaryCheckedProof)
append([[Startline, Assumption, assumption]], [TemporaryCheckedProof], CheckedProof).

Upvotes: 0

Views: 52

Answers (1)

TA_intern
TA_intern

Reputation: 2422

A Prolog list is by definition a compound term with two arguments. The first argument is the first element of the list, and the second argument is the rest of the list.

Prolog provides the notation List = [H|T] where you have a list List that has a first element H and the rest of the list in T.

In other words, you don't have to do anything special to add an element at the front of an existing list. Here:

?- Old = [a,b,c], New = [hello|Old].
Old = [a, b, c],
New = [hello, a, b, c].

Not sure what you are doing in your updateList predicate but it seems unnecessary.

Upvotes: 1

Related Questions