prolog

Prolog Tree Traversal

微笑、不失礼 提交于 2019-12-24 05:05:11
问题 Good Day, I am trying to write a Prolog program that given a tree with a functor of a: start(a(f,2,9), X). I want it to square any values inside so that it yields: X = a(f,4,81). I have code that squares numbers in a list already that works. Here's what I have so far: start([],[]). start(Tree, []) :- Tree =.. [P|C], write(P), nl, write(C), nl, squareMe([P|C], []). squareMe([X|T], [Y|Result]) :- % I think the problem is here atom(X), Y=X, squareMe(T, Result). squareMe([X|T], [Y|Result]) :-

Prolog - Balanced tree or not

試著忘記壹切 提交于 2019-12-24 05:03:10
问题 I want to write a program that tells me if a tree is balanced or not. In this case balanced means same height or a height difference of 1. This is what I've written so far but it doesn't work for the height difference of 1. Why? balanced(l(_)). balanced(b(B1, B2)):- height(B1,H), height(B2,H), balanced(B1), balanced(B2). balanced(b(B1,B2)):- height(B1,H + 1), height(B2,H), balanced(B1), balanced(B2). balanced(b(B1,B2)):- height(B1,H), height(B2,H + 1), balanced(B1), balanced(B2). 回答1: H + 1

Prolog - Balanced tree or not

孤街醉人 提交于 2019-12-24 05:03:03
问题 I want to write a program that tells me if a tree is balanced or not. In this case balanced means same height or a height difference of 1. This is what I've written so far but it doesn't work for the height difference of 1. Why? balanced(l(_)). balanced(b(B1, B2)):- height(B1,H), height(B2,H), balanced(B1), balanced(B2). balanced(b(B1,B2)):- height(B1,H + 1), height(B2,H), balanced(B1), balanced(B2). balanced(b(B1,B2)):- height(B1,H), height(B2,H + 1), balanced(B1), balanced(B2). 回答1: H + 1

Which list item is the most common

微笑、不失礼 提交于 2019-12-24 04:51:59
问题 I'm trying to find the most common list item common([b,a,a,a,c,d,b,f,s,f,s,f,s,f,s,f,f],R) so the result should be R=f, I was thinking if we take the list , go to the end of the list take el=b ,num1=1 then go back to the beginning and compare if b=b ,num1=num1+1 else a!=b then if num2=num2+1 , num1>num2 recursion else el=a or something like this, but i had some difficulty transforming it into Prolog. insert_sort sorts the list , but for some interesting reason if i use las(X,Y) (I override

Invalid Result in Prolog Program

只愿长相守 提交于 2019-12-24 04:44:06
问题 My program is intended to get rid of repeating elements in a list. I think its correct. I tried to make it work by putting cuts, but it didn't give correct results. member(X,[X|_]). % If X is Head, it is a member member(X,[_|T]) :- member(X,T). % If not, Recursively check the tail. remove_duplicates([],[]). remove_duplicates([H|T],R):-member(H,T),remove_duplicates(T,R). remove_duplicates([H|T],[R|REM]):-remove_duplicates(T,REM). However I am getting results: I = [_G2608, _G2611, _G2614|_G2615

A DCG that matches the rest of the input

喜你入骨 提交于 2019-12-24 04:41:25
问题 This is the predicate that does what it should, namely, collect whatever is left on input when part of a DCG: rest([H|T], [H|T], []). rest([], [], []). but I am struggling to define this as a DCG... Or is it at all doable? This of course is not the same (although it does the same when used in the same manner): rest([H|T]) --> [H], !, rest(T). rest([]) --> []. The reason I think I need this is that the rest//1 is part of a set of DCG rules that I need to parse the input. I could do phrase(foo

Predicate in prolog

左心房为你撑大大i 提交于 2019-12-24 04:32:23
问题 I need to define a predicate in prolog which takes a list as input and sums the squares of numbers >= 5 and subtract sum of absolute value of numbers <=2. This is what I have currently :- pred([], 0). pred([Head|Tail], Result) :- gr85(Head), pred(Tail, Total), Result is Head*Head + Total. pred([Head|Tail], Result) :- leq2(Head), pred(Tail, Total), Result is Total - Head. gr85(Number):- Number >= 5. leq2(Number):- Number =< 2. My question is how do I exclude anything between 2 and 5. If I

Permutation Prolog

℡╲_俬逩灬. 提交于 2019-12-24 04:15:26
问题 I am trying to make a list*list of all permutations from 1 to N Example: perm(3, X). -> X = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] I am instead getting X = [1, 2, 3] X = [1, 3, 2] X = [2, 1, 3] X = [2, 3, 1] X = [3, 1, 2] X = [3, 2, 1] and having to keep hitting next. My question is how would I put all values of X into a list like the example run that I want. Here is my existing code: permHelp([],[]). permHelp(List,[H|Finish]):-delete(H,List,Rest),permHelp(Rest

Recursion in Prolog - Finding Path Between Cities

一个人想着一个人 提交于 2019-12-24 04:15:07
问题 I'm trying to work my way through the exercises at the bottom of this page and I find myself utterly confused on number 3. We are given the following knowledge base of travel information: byCar(auckland, hamilton). byCar(hamilton, raglan). byCar(valmont, saarbruecken). byCar(valmont, metz). byTrain(metz, frankfurt). byTrain(saarbruecken, frankfurt). byTrain(metz, paris). byTrain(saarbruecken, paris). byPlane(frankfurt, bangkok). byPlane(frankfurt, singapore). byPlane(paris, losAngeles).

Using cuts in prolog to select facts from database

坚强是说给别人听的谎言 提交于 2019-12-24 03:59:12
问题 I'm supposed to use Prolog cuts to get the first , the second and the last fact from the facts database , I found a way to get the first and the second but I can't find a solution for retrieving the last fact here is an example : P(jack). P(john). P(alice). P(sarah). P(kyle). Selecting the first fact only : first(X):-P(X),!. Selecting the second fact only : second(Y):-P(X),P(Y),X\=Y,P(Y),!. Selecting the last fact only : ? 回答1: I can't see a way without using negation, an accumulator, and the