prolog-dif

What is never equal to itself?

ぃ、小莉子 提交于 2019-12-23 22:25:18
问题 Is there value in Prolog that is not equal to itself? I write answer to some question about min of tree and this answer also says that if tree is empty min is null. Sounds good idea first but now when I think it sounds like bad idea. It is kinda OK if null <> null , no problem. But in Prolog I see null is just atom so.... ?- null = null. true. ?- null == null. true. ?- dif(null, null). false. How can I make some term in Prolog that always say: ?- dif(Something, Something). true. But if it is

Remove both the value and all duplicates of that value in a list in prolog

若如初见. 提交于 2019-12-23 15:33:56
问题 I'm having some trouble removing values from a list in prolog. I have a list of colors and I want to add a list of colors to it and keep all the values that have no duplicate and remove the rest. [green, red, blue, purple, yellow, brown, orange, black, purple] so purple appears twice in this list and I want to remove both of them. This is the list I want to be returned. [green, red, blue, yellow, brown, orange, black] I currently have this to remove all the duplicates but I can't get both

Remove both the value and all duplicates of that value in a list in prolog

给你一囗甜甜゛ 提交于 2019-12-23 15:33:33
问题 I'm having some trouble removing values from a list in prolog. I have a list of colors and I want to add a list of colors to it and keep all the values that have no duplicate and remove the rest. [green, red, blue, purple, yellow, brown, orange, black, purple] so purple appears twice in this list and I want to remove both of them. This is the list I want to be returned. [green, red, blue, yellow, brown, orange, black] I currently have this to remove all the duplicates but I can't get both

Solving a textual logic puzzle in Prolog - Find birthday and month

让人想犯罪 __ 提交于 2019-12-22 11:35:37
问题 I'm reading the "7 Languages in 7 Days"-book, and have reached the Prolog chapter. As a learning exercises I'm trying to solve some textual logic puzzles. The puzzle goes as follow: Five sisters all have their birthday in a different month and each on a different day of the week. Using the clues below, determine the month and day of the week each sister's birthday falls. Paula was born in March but not on Saturday. Abigail's birthday was not on Friday or Wednesday. The girl whose birthday is

How to access list permutations in prolog?

风格不统一 提交于 2019-12-18 13:38:30
问题 I want to access list permutation and pass it as argument to other functions. This is the permutation code: takeout(X,[X|R],R). takeout(X,[F|R],[F|S]) :- takeout(X,R,S), write(S). perm([X|Y],Z) :- perm(Y,W), takeout(X,Z,W). perm([],[]). 回答1: To start with, let's redefine your predicates so they don't do any unnecessary I/O: takeout(X,[X|R],R). takeout(X,[F |R],[F|S]) :- takeout(X,R,S). perm([X|Y],Z) :- perm(Y,W), takeout(X,Z,W). perm([],[]). Now you have what could be considered a "pure"

filter list into separate lists

大憨熊 提交于 2019-12-18 09:01:43
问题 I need to filter the list [#,d,e,#,f,g] such that I get the output as [[d,e],[f,g]] , I am stuck while creating a new list every time I encounter '#' is there a way to do this? I tried the code below, filterL([],List) :-[]. filterL([Head|Tail],X) :- ( Head \='#'-> append(X,Head,List), filterL(Tail,List) ; filterL(Tail,X) ). 回答1: Your problem is not very well defined. Are empty sequences allowed or not? Shall [#] be related to [[],[]] (there is an empty sequence before and after) or [] ? You

Prolog separating into two lists issue

*爱你&永不变心* 提交于 2019-12-17 21:21:07
问题 I have a prolog assignment. I need to look at the first item in a list, see if its following items are the same until they are not and separate the lists by the first item and its duplicates. e.g if my list was a,a,a,b,c it would separate it into first: a,a,a. second: b,c. My current solution works except that the final matching item comes into the second list, not the first. I can't seem to think of a way to get it to appear in the first list instead. grab([],[],[]). grab([A,A|L],[A|L2],Rest

Simple Prolog delete from list

匆匆过客 提交于 2019-12-17 17:00:59
问题 (This is NOT a coursework question. Just my own personal learning.) I'm trying to do an exercise in Prolog to delete elements from a list. Here's my code : deleteall([],X,[]). deleteall([H|T],X,Result) :- H==X, deleteall(T,X,Result). deleteall([H|T],X,[H|Result]) :- deleteall(T,X,Result). When I test it, I first get a good answer (ie. with all the Xs removed.) But then the backtracking offers me all the other variants of the list with some or none of the instances of X removed. Why should

Prolog: a person is a sibling of himself?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 02:32:07
问题 I'm having some trouble understanding why my code in prolog does something based on the order I put my rules in. Here is my database: parent(tom, bob). parent(tom, liz). parent(mary, bob). parent(mary, liz). male(tom). male(bob). female(mary). female(liz). And here are the rules: %difference(X, Y) ==> Predicate to check if two people X and Y are not the same person. difference(X, Y) :- \==(X, Y). father(X, Y) :- male(X), parent(X, Y), difference(X, Y). mother(X, Y) :- female(X), parent(X, Y),

Prolog: check against duplicates in a list

怎甘沉沦 提交于 2019-12-13 15:22:23
问题 Write a predicate allDistinct/1 whose parameter is a list (of symbols) and which succeeds if all symbols in the list are different. notin(A,[]). notin(A,[B|C]) :- A\=B, notin(A,C). allDistinct([]). allDistinct([_]). allDistinct([A|B]) :- notin(A,B), allDistinct(B). 回答1: Following up on the previous sketch by @whd we can proceed like this. Based on iwhen/2 we can succinctly define distinct/1 like so: :- use_module(library(lists), [same_length/2]). distinct(Es) :- iwhen(ground(Es), (sort(Es,Fs)