prolog

Prolog getting frequency of list

↘锁芯ラ 提交于 2020-01-03 17:08:22
问题 Working on a prolog assignment. I have a structure cnt(letter,number). I need to return a list of cnt where each cnt is the number of times each character appears (assuming that each item has already been sorted to place the same item one after each other). I have this so far: cnt(letter,number). freq([],[]). freq([A|L],Y) :- grab(A,Init,_), freq(L,[Init|Y]). grab works correctly takes a list of items and returns the list of the first duplicates as Init e.g grab([a,a,a,b,c], Init, Rest). will

Program to find every list of X in Prolog

青春壹個敷衍的年華 提交于 2020-01-03 16:01:30
问题 I am starting on learning Prolog. This program tries to get all occurrences of a given element: occurences(_, [], Res):- Res is []. occurences(X, [X|T], Res):- occurences(X,T,TMP), Res is [X,TMP]. occurences(X, [_|T], Res):- occurences(X,T,Res). But here is the error: ?- occurences(a,[a,b,c,a],Res). ERROR: is/2: Arithmetic: `[]/0' is not a function ^ Exception: (11) _G525 is [] ? creep Exception: (10) occurences(a, [], _G524) ? creep Exception: (9) occurences(a, [a], _G524) ? creep Exception:

Why does Prolog crash in this simple example?

旧城冷巷雨未停 提交于 2020-01-03 15:54:54
问题 likes(tom,jerry). likes(mary,john). likes(mary,mary). likes(tom,mouse). likes(jerry,jerry). likes(jerry,cheese). likes(mary,fruit). likes(john,book). likes(mary,book). likes(tom,john). likes(john,X):-likes(X,john), X\=john. Hi there, above is a very simple prolog file, with some facts and only one rule: John likes anyone who likes him. But after loading this file and ask Prolog the following query: likes(john,X). The program crashes. The reason is somehow prolog gets stuck at likes(john,john)

How to create a list from facts in Prolog?

好久不见. 提交于 2020-01-03 13:32:07
问题 There are these facts: man(john). man(carl). woman(mary). woman(rose). I need to create the predicate people(List), which returns a list with the name of every man and woman based on the previous facts. This is what I need as output: ?- people(X). X = [john, carl, mary, rose] And here is the code I wrote, but it's not working: people(X) :- man(X) ; woman(X). people(X|Tail) :- (man(X) ; woman(X)) , people(Tail). Could someone please help? 回答1: Using findall/3: people(L) :- findall(X, (man(X) ;

How to create a list from facts in Prolog?

五迷三道 提交于 2020-01-03 13:32:06
问题 There are these facts: man(john). man(carl). woman(mary). woman(rose). I need to create the predicate people(List), which returns a list with the name of every man and woman based on the previous facts. This is what I need as output: ?- people(X). X = [john, carl, mary, rose] And here is the code I wrote, but it's not working: people(X) :- man(X) ; woman(X). people(X|Tail) :- (man(X) ; woman(X)) , people(Tail). Could someone please help? 回答1: Using findall/3: people(L) :- findall(X, (man(X) ;

Solution to Smullyan's numerical machines

江枫思渺然 提交于 2020-01-03 10:12:09
问题 Here I propose to find a solution to Smullyan's numerical machines as defined here. Problem statement They're machines that take a list of digits as input, and transform it to another list of digits following some rules based on the pattern of the input. Here are the rules of the machine given in the link above, expressed a bit more formally. Let say M is the machine, and M(X) is the transformation of X. We define a few rules like this: M(2X) = X M(3X) = M(X)2M(X) M(4X) = reverse(M(X)) //

Are cuts that bad in programming? [closed]

故事扮演 提交于 2020-01-03 07:37:11
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . Hey guys I'm taking an AI course this semester in which we are learning Prolog. Our lecturer has told us to try and avoid using cuts in our assignment, however, for a couple of the questions I can't seem to avoid using them. I'm just curious why are cuts considered a sin

Passing arbitrary-sized integers from Prolog to C

独自空忆成欢 提交于 2020-01-03 07:20:29
问题 Right now, I'm learning how to interface SICStus Prolog with C code. I would like to have/use/see a C implementation of "Hamming weight" of arbitrary-sized integers in SICStus Prolog version 4. It seems to me that I need C functions for testing term types (SP_is_integer) and C functions for accessing Prolog terms (SP_get_integer, SP_get_integer_bytes). However, I'm not sure how to use SP_get_integer_bytes in a portable, robust fashion. Could you please point me to some well-crafted solid C

Prolog Parent relation using only brother and sister rules

允我心安 提交于 2020-01-03 04:34:08
问题 this is my first time using Prolog, and I was wondering if anyone could give me some advice on my logic: male(jerry). male(stuart). male(warren). male(peter). female(kather). female(maryalice). female(ann). brother(jerry,stuart). brother(jerry,kather). brother(peter, warren). sister(ann, maryalice). sister(kather,jerry). parent_of(warren,jerry). parent_of(maryalice,jerry). This is part of a homework assignment, and we are only allowed to use the above facts. In order to know that warren and

Using a single clause compute whether the sum of any three members of a list is equal to given value

爷,独闯天下 提交于 2020-01-03 01:22:31
问题 We are not supposed to use any of the functions other than the ones listed below: A single clause must be defined (no more). + , ; . ! :- is Lists Head and tail syntax for list types Variables For example sumlists([1,2,3,5,7],11) then the program execution should print TRUE . Because 1+3+7 (any three)=11 (given N value). 回答1: Ideally, we either get an element or don't, as we go along the input list; and we stop either on having reached the needed sum, or having surpassed it, or when the list