swi-prolog won't answer query - prolog

I'm trying to write a query that will make sure an element is present in a list of lists, I tried this implementation:
membernested(E,[H|T]):-member(E,H).
membernested(E,[H|T]):-membernested(E,[T]).
but Prolog won't answer the query, any thoughts?

Change you second clause to:
membernested(E,[H|T]) :- membernested(E,T).
The tail of the list [H|T] is T, not [T]. There's no need to enclose it in another list.

Related

prolog, checking if all members of a list are unique without built-in predicates

I want to write the rule unique(List) that checks if all elements in List are unique. I'm not allowed to use member, but I am allowed to use 'not'. Therefore I wrote the rule 'member' myself.
I wrote this:
member(Element, [Element|_]).
member(Element, [_|List]) :-
member(Element, List).
unique([H|T]) :-
not(in_list(H, T)),
unique(T).
The member-rule is working, it checks if Element is a member of List. But the unique- rule doesn't work. For the unique-rule is was expecting it would check if H was in T and then do the same for the Header of the Tail and so on. The 'not' makes from a false statement a True-output.
When I run this rule with query ?-unique([1,2,3,4,5]) it gives False. So what's my mistake?
I just discoverd the solutions a few moments later.
I added this above:
unique([]).
unique([_,[]]).

how to use maplist and predicates

If a predicate has 2 arguments it can be called like this:
maplist(member(#),List_of_lists,New).
But what if I wanted to call it the other way around, iterating through a list of values to see if any of them belonged in a list I had already? How can i make it so that the elements of the list are not the last argument of the function but rather the first or the second or the third?
EDIT: That code above was an example not what I actually wanted.
pal_pos_esps(Letras, Pals_Possiveis, Espacos):-
maplist(palavras_possiveis_esp(Letras,Espacos,_,Pals_Possiveis),Espacos,Pals_Possiveis).
Espacos is a list of list: I want to call each of those lists as the third argument of the predicate. How can I do so?
How can i make it so that the elements of the list are not the last argument of the function but rather the first or the second or the third?
You could use a lambda construct. In SWi-Prolog there are either library(yall) or library(lambda) (available after you install it with ?- pack_install(lambda).).
Let's see a basic usage, allocating a matrix NxN, which requires swapping the arguments order of length/2.
With the autoloaded library(yall):
matrix(N,M) :- length(M,N), maplist([Row]>>length(Row,N),M).
Using library(lambda):
:- use_module(library(lambda)).
matrix(N,M) :- length(M,N), maplist(\Row^length(Row,N),M).

List check that elements are different

I would like to write a predicate different_from(Xs,X) which is a check that succeeds if and only if X is different from all the elements of the list Xs.
So the query
different_from([3,2,5],4)
should succeed but this following query should fail:
different_from([3,2,5],2)
Since the predicate is a check it should not instantiate either of its arguments.
thank you in advance for any help you can provide.
This is a very simple rule consisting of two clauses:
The first clause says that different_from is successful when the list is empty,
The second clause says that different_from is successful when the first element of the list does not match the element being searched, and also when different_from for the tail is successful as well.
Here is the same thing written in Prolog syntax:
different_from([], _).
different_from([H|T], E) :- E \= H, different_from(T, E).

prolog expanding predicate to iterate for multiple results, combining / resolving result sets

I have a predicate "lookupOptions" which returns one by one some lists (Menus).
I'm trying to get it to satisfy the case of multiple inputs. I can return a single set of options as follows, by reading the head of the "list_places" list.
find_options(Restaurant,Town,Menu) :- lookupOptions(Restaurant,H,Menu), list_places(Town,[H|T])
But, I'm not able to get it to iterate.
I have tried a lot of things, these were my best efforts so far.
a) standard enough iteration, but it wont resolve ...
doStuff(X,[],_).
doStuff(Restaurant,[H|T],_):- lookupOptions(Resturant,H,_), doStuff(Restaurant,T,_).
find_options(Restaurant,Town,Menu) :- doStuff(Restaurant,[H|T],Menu), list_places(Town,[H|T]).
b) expanding the goal predicate ...
find_options(_,Town,[H|T],_)
find_options(Restaurant,Town,Menu) :- find_options(Restaurant,Town,[],Menu).
find_options(Restaurant,Town,X,Menu) :- list_places(Town,X).
find_options(Restaurant,Town,[H|T],Menu) :- lookupOptions(Restaurant,[H],Menu), find_options(Restaurant,Town,T,Menu).
Would either of these work ? if the pattern was written correctly. Or if there was an appropriate cut put in place?
Any help most appreciated ...
It's no clear on what you want iterate. Prolog uses backtracking to examine all alternatives, then you should start backtracking if you are after some alternative, or use the all solutions family.
Now I think you want simply declare there could be more find_options(Restaurant,Town,Menu). Then try replacing the head match [H|T] with this:
find_options(Restaurant,Town,Menu) :-
lookupOptions(Restaurant,H,Menu),
list_places(Town, Places),
member(H, Places).
BTW T is a singleton in your original rule. This could be a hint for the need of generalize it.

Convert list into functor parameter

I got stuck to implement a logic. At some instance in my program I have a list say named as List.
The length of this List is variable and I don't know in advance. Now I have to pass this list in a functor to create a fact and I am unable to implement it. For eg:
if List is [first] then it should add the fact functor(first).
if List is [first,second] then it should add the fact functor(first,second).
if List is [first,second,third] then it should add the fact functor(first,second,third).
and so on...
I was trying by =.. but here I am unable to map that variable length constraint. For fixed length I am able to perform but I don't know in advance that how many elements will be there in list.
Any suggestions to implement this logic. Thanks.
I don't quite understand your problem with =.. but this worked for me:
assert_list(List) :-
Term =.. [my_functor|List],
assert(Term).
Note that I use my_functor instead of simply functor because functor/3 is a built-in predicate so you cannot assert ternary functor facts (functor(first, second, third)).
Calling it:
?- assert_list([first,second,third]).
true.
Checking that it works:
?- listing(my_functor).
:- dynamic user:my_functor/3.
user:my_functor(first, second, third).
true.
Note that technically, the different n-ary my_functor/n predicates are not the same predicates. You must use different queries in your program for each n. To circumvent this, you could simply assert the list as one and only argument of my_functor:
?- List = [first, second, third],
assert(my_functor(List)).
true.
?- listing(my_functor).
:- dynamic user:my_functor/3.
user:my_functor([first, second, third]).
true.
My SWI-Prolog version is 5.7.5.

Resources