prolog

What is the difference in execution if the cut '!' is present?

ε祈祈猫儿з 提交于 2019-12-23 07:47:40
问题 counter([],[]). counter([H|T],[[H,C1]|R]) :- counter(T,[[H,C]|R]),!, C1 is C+1. counter([H|T],[[H,1]|R]) :- counter(T,R). What is the effect of the "!" as I'm getting the same output for an input in both the above and below code? counter([],[]). counter([H|T],[[H,C1]|R]) :- counter(T,[[H,C]|R]),C1 is C+1. counter([H|T],[[H,1]|R]) :- counter(T,R). I'm new to Prolog. 回答1: What is the effect of the "!" The cut prunes the search space. That is, in an otherwise pure and monotonic program, the cut

Does an element exists in a list of lists?

北慕城南 提交于 2019-12-23 06:57:30
问题 I want to find if a given element exists in a list of lists. I am only getting true if the element exists somewhere is the first list of lists. Any advice? memberlist(X,[[X|T1]|T2]). memberlist(X,[[H|T1]|T2]) :- memberlist(X,[T1|T2]). 回答1: The problem is that [[H|T1]|T2] only matches given the first list has at least one element. Indeed: [[],[1,4],[2,5]] for instance does not unify with [[H|T1]|T2] . So you can solve the problem by adding a clause: memberlist(X,[[]|T2]) :- memberlist(X,T2).

How to create family relation logic?

╄→尐↘猪︶ㄣ 提交于 2019-12-23 05:23:16
问题 I'm trying to make this but for a different language. In this language there are different kinds of names for uncles and aunts. We call paternal aunt something else and maternal aunt something else. I came across a graph database 'neo4j'. I created 5 members. I got this approach to work just like I want. But the problem in this is that I've to create n * (n-1) relationships. I'm create a full tree here and not just 5 members of a family. Also, this is more like brute force. I'm creating all

How to do arithmetic expression evaluation in prolog?

£可爱£侵袭症+ 提交于 2019-12-23 03:46:14
问题 I am trying to solve an arithmetic expression in prolog (implementation - eclipse prolog). The arithmetic expression to be solved is like this: A * (C + B * X) + D * X = E X is the value to be computed, and all others (A,B,C,D,E) are all numbers. For example: 5 * (3 + 2*X) + 2*X = 39, on computing should assign X with the value 2. The query(goal) that would be entered into Prolog would take the form: ?- compute( 5*(3+2*X)+2*X = 39, Result). The 'Result' and value of 'X' should be tied

Logic Dots puzzle problems with SWI-Prolog

霸气de小男生 提交于 2019-12-23 03:39:14
问题 I am working on a logic game named "Logic Dots". The game is a 3*3 grid with numbers surrounding each row and column. The rule is easy that the sum of each row and column should equal to those numbers. Below is my code, it is changed from clpfd sokoku. But result is something wrong. Code: :- use_module(library(clpfd)). kakuro(Rows) :- length(Rows, 3), maplist(length_list(3), Rows), append(Rows, Vs), Vs ins 0..1, Rows = [A,B,C], sum(A,#=,1), sum(B,#=,1), sum(C,#=,1), transpose(Rows, Columns),

Prolog grandfather(i,i)

泄露秘密 提交于 2019-12-23 03:13:13
问题 A (very) strange story: I married a widow(W) who has a daughter(D). My father(F) married my stepdaughter (D). My wife gave birth to a son(s1). The wife of my father (the stepdaughter) also had a son (s2). The goal of this project is to input: grandfather(i,i). and return yes in prolog. Here is what I have so far: %facts father(f,i). husband(i,w). husband(f,d). mother(w,d). mother(w,s1). father(i,s1). mother(d,s2). father(f,s2). %rules father(X,Y) :- f_in_law(X,Y). father(X,Y) :- husband(X,Z)

Getting hold of a variable in complex compound term in Prolog

这一生的挚爱 提交于 2019-12-23 03:07:22
问题 I have a Prolog sentence parser that returns a sentence (passed into it as a list) split into two parts - a Noun_Phrase and a Verb_Phrase . See example below: sentence(Sentence, sentence(np(Noun_Phrase), vp(Verb_Phrase))) :- np(Sentence, Noun_Phrase, Remainder), vp(Remainder, Verb_Phrase). Now I want to take the Noun_Phrase and Verb_Phrase and pass them into another Prolog predicate, but first I want to extract the first term from the Verb_Phrase (which should always be a verb) into a

Splitting a sentence without any whitespace/seperators into a sentence with whitespace

纵然是瞬间 提交于 2019-12-23 02:29:06
问题 I'm working on an end-of-semester project for a Programming languages course. The assignment is given below. I'm finishing writing it in Java and I'm having a lot of trouble writing in Prolog. I've been having a lot of trouble with Prolog so this question is as much looking for help with the assignment as it is trying to understand Prolog more. Any help that I can get would be GREATLY appreciated A sentence contains words, all occurring in a dictionary, that happen to be concatenated without

Hangman Game in SWI Prolog (Enchance) [duplicate]

淺唱寂寞╮ 提交于 2019-12-23 01:59:11
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Hangman Game in SWI Prolog I'm trying to enchance a simple hangman game in SWI Prolog with the following: 1) By keeping up with the WRONG letters that have been guessed so far. If the user guesses a letter that has already been guessed wrong, the program should say 'You guessed that!' and just continue the game without increasing the counter. 2) Lastly, add a counter that counts the number of incorrect guesses

How integer suspension will be handled when it is used in head of a condition

若如初见. 提交于 2019-12-23 01:27:09
问题 I have the following conditions over two variable A and B : [A,B] #:: 1..10, (A #= 3) or (B #= 3), ((A #> 3 or B #>3) -> % expression 1 ; % expression 2 ) %cntd The problem is in line 2, the solver doesn't know about the value of A and B , how to decide which branch of condition will be continued without specifying the value of the variables at line 2? The reasonable act is to decide on this branch based on the value of the variables when the solver traverse the possible values for the