Prolog recursively checking if an item is in a list - list

So I am trying to write a piece of code that checks to see if an item is in a list or not.
is_member(_,[]).
is_member(X,[X|_]).
is_member(X,[_|tail]):- is_member(X,tail).
this is currently what I have for the code. It works if the item is in the first position but doesn't check the rest of the list. Can anyone help me figure out what i'm doing wrong? Thanks.

Your predicates faces the following two problems:
The base case is_member(_,[]). will always return true for an empty list, which is not correct; and
Your variable should start with an Uppercase character: Tail.
An example how to implement a predicate that solves these two issues:
is_member(X,[X|_]).
is_member(X,[_|Tail]):- is_member(X,Tail).

Related

If-else statement trouble

My project uses an if-else statement to determine if a random image from a list of images matches to an image in another list. I'm trying to find a piece of code that will allow me to set the if-else statement so when it asks: if randomImage == list[?]. In the question mark I need code that will go through the entire list and see if the randomImage matches from ANY of the elements in the list. Here's a snippet of code: trash[randomTrash] generate a random image from the list trash. I need it so it checks if the random image of trash is equal to an image in another list. It needs to go through recycle list and determine if an element is equal to it.
There is probably an easier way to do this depending on your project specifics, but you should be able to use a for loop that loops through each element in your list.
for (int element = 0; element < list.length; element++) {
if (randomImage == list[element]) {
// randomImage matches with an element in the list. Assuming you are using some boolean variable 'match' which is initialized to be false.
match = true;
}
}
it's really helpful if you tag the language you're using, so people know how exactly to address the issue in particular.
The most common and straightforward approach you would see is looping through the whole list by index, and comparing each image to the one from it, and that's a solid working one.
If you're on a language supporting list comprehension you could take an approach similar to this,
[x for x if x==image...]
then check if the list is empty for your if/else condition.
Please let us know in particular if it's something more specific you're looking for.

Returning an index of an alphabetical list python

So I'm working on a homework for a beginning python class. I'm asked to write a function taking two parameters, one a string and one of a list in alphabetical order. The function is to return the integer index as to where the string would be placed in order for the list to stay alphabetized.
I am not allowed to insert anything into the list, or append the list in any matter (I tried just adding the string to the list, resorting and then returning the index for where the string now lived) All the function is to return is the integer index value. I could use some direction as to where to start without using an insert and resorting... Thanks.
Because I dont want to write your homework for you, here is one way to do it in pseudo-code:
def insert_index(string, list)
for every item in your list:
if the item is greater than your string:
return index of item
else:
go to next item
weirdly enough, because of the way python is written, this is very close to actual code...
because strings are comparable, you can actually do something like 'a'<'b' and return a valid bool. and since your list is alphabetical, as soon as you hit an item that is greater than your string, then you know thats where you want your string to go.
also, it would be useful to use enumerate function for your loop structure (HINT HINT)
I would iterate over the list and compare the current string in the list with the string you are trying to insert ( < > comparators work on strings, with 'b' > 'a'. Without giving too much away, take advantage of the fact that the list you are given is already in alphabetical order to determine which index the passed in string would be placed in.
One cool thing in python is that letters are of a higher value if they are farther along in the alphabet. For example, 'b' > 'a'. In fact, you can type 'b' > 'a' into an interpreter and it will tell you 'true'. So simply loop through the alphabetical list comparing the first letter in the list item to the first letter in the string.
Something like this (I haven't checked it, so it may not work perfectly, but try to get the gist of it)
for i in range(0,len(list)):
if (list[i][0] < str[0]):
print(i)
break

Prolog Combining Two Lists

I am new to prolog and would appreciate any help on the following question:
I need to write a program that accepts two lists and appends the second to first and displays this new list and its length. I know that prolog might have some built in functions to make this all easier...but I do not want to use those.
eg: newlist([a,b,c],[d,e,f],L3,Le). would return L3=[a,b,c,d,e,f] and Le=6
Here is what I have so far:
newlist([],List,List,0)
newlist([Element|List1],List2,[Element|List3],L) :- newlist(List1,List2,List3, LT), L is LT + 1.
This does the appending correctly but I can only get the length of the first list instead of the combined list. Is there a way for me to add the second list's length to the first to get the combined list length?
Thanks, and sorry if this question is rather easy...I am new.
Is there a way for me to add the second list's length to the first to get the combined list length?
You should replace:
newlist([],List,List,0).
with:
newlist([],List,List,X):-length(List,X).

Reading and storing all words of file in prolog

I am a newbie to prolog, till now I am able to read all words of file, displayed them one by one, now I want to store them in a list(one by one, as I soon as I am displaying them). All logic for append given everywhere, append content of two lists in an empty list. For example
append(new_word,word_list,word_List), intially my word_list is empty, so everything fine, but afterwards it says no, and stop at that point.
Need help to be able to store element in list one by one.
You can use difference lists :
file_to_list(W, L) :-
read_word(Word),
append_dl(W, [Word|U]-U, Ws),
!, file_to_list(Ws, L).
file_to_list_1(Ws, Ws).
append_dl(X-Y, Y-Z, X-Z).
You call file_to_list(U-U, L-[]) to get the list of words. There is no slowdown but takes more inferences than CapelliC's code (one per word).

Prolog permutations with repetition

I'm having a hard time wrapping my head around the concept of logic programming. I'm trying to get all permutations with repetition into a give list.
I can put what I have, but I don't know what I'm doing!
perms_R(List,[]).
perms_R([X|Xt],[Y|Yt],Out) :- perms_R([Y|Xt],Yt),perms_R(Xt,[Y|Yt])
.
The idea was to go through each element in the second list and put it in my first list. I'm trying to figure this out, but I'm stuck.
I need to call perms_R([a,b,c,d],[1,2,3,4]). and get:
1,1,1,1
1,1,1,2
1,1,1,3
1,1,1,4
1,1,2,1
etc....
I understand the first list seems useless and I could just do it with a list length, but I actually need it for the remainder of my code, so I'm trying to model this after what I need. Once I get past this part, I will be putting extra logic in that will limit the letters that can be replaced in the first list, but don't worry about that part!
What you are looking for is not a permutation. You want to create a list of a given size using items from a given set.
You may do it with this snippet:
perms_R([], _).
perms_R([Item|NList], List):-
member(Item, List),
perms_R(NList, List).
You would need to pass a semi instantiated list and the source items:
perms_R([A,B,C,D],[1,2,3,4]).