Prolog: append a list to itself - prolog

suppose I have a list ListSum, and I want to append a new list to ListSum recursively, like
appList(ListSum):-
%%generate a list: ListTemp,
append(ListTemp,ListSum,ListSum),
appList(ListSum).
but append(ListTemp,ListSum,ListSum) didn't work in the way i wanted.
Can anyone help me out?
Cheers

You have to understand the concept of unification (or actually "matching" as implemented in Prolog). You can't bind two or more values to the same variable. Variables in Prolog once matched persisted its value until the final goal achieved, or fails somewhere. After that, if there're more possibilities then the variable is re-instantiated with another value and so on.
For example, if I query appList([]), then the append would be tested to match as:
append(ListTemp,[],[])
If ListTemp isn't empty list, this clause would fail because the semantic of append is "append the first argument with second, both are lists, resulting in the third". The recursive call to appList(ListSum) would be called as appList([]) since ListSum is matched with [] previously, resulting in infinite recursion (fortunately, if ListTemp isn't [], this won't be reached).
You must have two arguments in the clause, where one is the original list, and the other is the resulting list. The first two argument of append is then ListSum and ListTemp (depends on the append order you want), while the third is the resulting list. Done, no recursion required.

here's a non-recursive solution, not sure why you even need recursion:
appself(L,X) :- append(L,L,X).

Related

How do I prevent to operate over an empty matrix or a matrix with empty columns or row?

In the problem that I want to solve a well defined matrix has no empty rows or columns.
For example the matrix [[],[]] is not valid.
When I call the function first_column, how do I prevent to execute it if the matrix that I send as an argument is not valid as defined before?
first_column([],[],[]).
first_column([[H|T]|Tail],[H|Col],[T|Rows]):- first_column(Tail,Col,Rows).
Technically, what you're asking can be done by testing for an end-condition of a list with one element, rather than an empty list, based on the specs you gave.
first_column([[H|T]],[H],[T]).
first_column([[H|T]|Tail],[H|Col],[T|Rows]):- first_column(Tail,Col,Rows).
However, beyond your specs, I suspect that you'll also need to "transfer" your final Col,Rows to end variables, something like:
first_column([[H|T]],C,R,[H|C],[T|R]).
first_column([[H|T]|Tail],[H|C],[T|R],Col,Rows):-
first_column(Tail,C,R,Col,Rows).
The modified predicate would be called with initial conditions, like
first_column(List,[],[],Col,Rows).

Prolog Sorting approach

Hı guys.I have a problem about prolog example.I have written following fact and rule to my database file but my database file doesn^t work while I compile it.Do you have any idea?
The error that I get is singleton variables
ordered([B]).
ordered(H|[H2|T]):-
H=<H2,
ordered([H2|T]).
The "singleton variable" warning is warning you that in the clause
ordered([B]).
the variable B only appears once, i.e., is not used to bind values between different calls, i.e., can serve no purpose. This is usually the result of a typo or from forgetting to make an intended change, so the top-level issues a warning. The usual way to use singleton variables for the easy of reading without triggering a warning is to prefix it with an underscore: _B.
But this is unrelated to the real problem with your code. A singleton variable will not cause any problems, it's just a matter of style or a way of warning of a typographical error. As #jol76 notes, the real problem with your code is that H|[H2|T] is not the correct notation for matching a list's first to elements and it's tail. The notation is [H,H2|T]. I think of the notation as [a,n,y,' ',n,u,m,b,e,r,' ',o,f,' ',e,l,e,m,e,n,t,s,' ',a,n,d|TheRest].
Most recursive rules have a couple of special/base cases and a general case.
You have two base cases:
ordered( [] ) . % the empty list is ordered
ordered( [_] ) . % a list consisting of a single item is ordered.
And the general case:
ordered( [A,B|C] ) :- % a list of two or more items is ordered, IF ...
A =< B , % - the first item is less than or equal to the second
ordered( [B|C] ). % - if the remainder of the list (less its head) is also ordered.
You might note that this won't actually sort the list for you: it will just indicate (via success or failure) whether or not the list is ordered.

Type error: Wrong object type. - Basic prolog error

I'm trying to learn prolog and i bumpt in this error which, i don't know why i get it so i am asking for your help.
code(TPROLOG#86):
trace
domains
item = integer
intList = item*
predicates
member(item,intList)
clauses
member(elm,[elm|_]).
member(elm,[_|T]):- %%% ***ELM is seen as wrong type, why?***
member(item,[T]).
goal
member(5,[1,2,3,4,5])
Any advice or hint is welcomed. Thank you.
You are confusing variables and atoms. Atoms start with a lower case letter, whereas variables start with an upper case letter.
Also, your member/2 definition seems wrong. It should read:
clauses
member(Elm,[Elm|_]).
member(Elm,[_|T]):-
member(Elm,T).
First clause matches the element with the head of the second list. Second clause skips the head of the second list and recursively calls member/2 to find another match in the tail of the list.

Prolog replace element in a list with another list

*Hi, i am trying to replace an element from a list with another list and im stuck when turbo prolog gives me syntax error at the case where if C=A-> put in result list(L1) the list that replace the element.
domains
list=integer*
element=i(integer);l(list)
lista=element*
predicates
repl(list,integer,list,lista)
clauses
repl([],A,B,[]):-!.
repl([C|L],A,B,**[l(|L1])**:- C=A,repl(L,A,B,L1),!.
repl([C|L],A,B,[i(C)|L1]):- repl(L,A,B,L1),!.
Thanks for help, problem solved (using dasblinkenlight code)
Try this:
concat([],L,L).
concat([H|T],L,[H|Res]) :- concat(T,L,Res).
repl([],_,_,[]).
repl([Val|T],Val,Repl,Res) :- repl(T,Val,Repl,Temp), concat(Repl,Temp,Res).
repl([H|T],Val,Repl,[H|Res]) :- repl(T,Val,Repl,Res).
I do not know if it is going to work in Turbo Prolog, but it works fine in SWI, and it does not use any built-in predicates.
concat/3 pair of rules concatenates lists in positions 1 and 2 into a resultant list in position 3.
The first repl deals with the empty list coming in; it is identical to yours, except it replaces singleton variables with underscores (a highly recommended practice)
The second rule deals with the situation where the value Val being replaced is at the head of the list; it replaces the values in the tail, and concatenates the replacement list Repl with the result of the replacement Res.
The last rule deals with the situation when the head value does not match the Val. It recurses down one level, and prepends the head of the initial list to the head of the result of the replacement.
As a side note, the cut operator ! is rarely necessary. In case of this problem, you can definitely do without it.

Are there alternative ways to display a list other than by using loop?

I know on how to display a list by using loop.
For example,
choice(a):-write('This is the top 15 countries list:'),nl,
loop(X).
loop(X):-country(X),write(X),nl,fail.
Unfortunately, I don't know on how to display list by using list. Anyone can guide me?
it's not very clear what it is that you're trying to achieve.
I'm not sure from your description whether you have quite got to grips with the declarative style of Prolog. When you wrote your rule for loop you were providing a set of conditions under which Prolog would match the rule. This is different from a set of procedural instructions.
If you want to collect all the countries into a list you can use the setof rule like follows
top_countries(Cs):-
setof(C, country(C), Cs).
This will return a list [] of the countries matched by the rule.
If you wanted to output each element of this list on a new line you could do something like the following recursive function.
write_list([]).
write_list([H|T]):-
write(H),nl,
write_list(T).
The first rule matches the base case; this is when there are no elements left in the list. At this point we should match and stop. The second rule matches (unifies) the head of the list and writes it to screen with a newline after it. The final line unifies the tail (remainder) of the list against the write_list function again.
You could then string them together with something like the following
choice(a):-
write('This is the top 15 countries list:'),nl,
top_countries(X),
write_list(X).
Things to note
Try not to have singleton variables such as the X in your choice rule. Variables are there to unify (match) against something.
Look into good declarative programming style. When you use functions like write it can be misleading and tempting to treat Prolog in a procedural manner but this will just cause you problems.
Hope this helps
write/1 doesn't only write strings, it writes any Prolog term. So, though Oli has given a prettier write_list, the following would do the job:
choice(Countries):-write('This is the top 15 countries list:'),nl,write(Countries).

Resources