prolog

Iteration over relations using a database

北城以北 提交于 2020-01-05 06:56:20
问题 I'm new to prolog. Consider the following format: carsCompany(Tel_Number, Manager, Company_Name, [new_cars(Car_Name,info(Color,Creator),Date_Creation)], [old_cars(Car_Name,info(Color,Creator),Date_Creation)] ]). I would like to create a two argument relation which gets a list of car names and a company and adds them to the company. update_company([Mazda],Company). I have the following db example: carsCompany(1234, Jujiro Matsuda, Mazda, [new_cars(mazda_3,info(Grey,Person1),26082016)], [old

How to add elements from sublists with 2 elements (the first element is a string and the second one a number)?

旧城冷巷雨未停 提交于 2020-01-05 06:28:13
问题 I'm working on a list that contains sublists with 2 elements each. The first element of each sublist is a string and the second one a number. [ [e, 30], [a, 170], [k, 15], [e, 50] ] I want to add all the numbers of each sublist. I tried this one: sum_fire([H|T],S):- flatten(H,L), sum_fire(T,S), L=[_H1|T1], sum(T1,S). but it's completely wrong, I think. How can i get this to work? 回答1: You just need to break out the string versus the number: sum_fire( [[_,N]|Tail], Sum ) :- sum_fire( Tail, S1

write a prolog program displaying an M*N grid of asterisk?

半世苍凉 提交于 2020-01-05 05:56:19
问题 I need to write a recursive predicate rectangle , such that rectangle(M, N) writes out a solid rectangle of size M x N of asterisks, i.e., there should be M rows and N columns in the rectangle. For example: ?- rectangle(3,8). ******** ******** ******** true So far I have the statement line that prints N asterisks on a line: line(0). line(N) :- write('*'), A is N-1 , line(A). I've tried everything, but I keep getting an infinite grid of asterisks. Here's what I've got so far: rectangle(0,0).

Breadth first search in prolog gives me an error

断了今生、忘了曾经 提交于 2020-01-05 05:45:11
问题 This is my code for breadth first search strategy in prolog : s(a, b). s(a, c). s(b, g). s(b, f). s(c, r). s(c, e). goal(g). solve( Start, Solution) :- breadthlirst( [ [Start] ], Solution). breadthfirst( [ [Node | Path] |_], [Node | Path] ) :- goal( Node). breadthfirst( [ [N | Path] | Paths], Solution) :- bagof([M,N|Path], ( s( N, M), \+ member( M, [N | Path] ) ), NewPaths), conc( Paths, NewPaths, Pathsl), !, breadthfirs( Pathsl, Solution); breadthfirst( Paths, Solution). But when I run this

Count empty list in Prolog list of list

為{幸葍}努か 提交于 2020-01-05 05:26:26
问题 Learning Prolog still, I made the predicate empties(L,R) that accepts a list of groups in L (i.e.: [ [ 2 ] ,[ 1 ] ,[ ] ] ) and the R indicates the number of empty groups within the group, so for this particular case it should go empties([[2],[1],[]],1) it gives 'yes' for this particular case, which seems to make sense. if i ask: empties([[],[2],[2],[]],X). it will answer X = 2 , which again makes sense, but when i replace the X with a 0, it gives a yes too, and i can't get a grasp on why. It

A prolog program that reflects people sitting at a round table

*爱你&永不变心* 提交于 2020-01-05 05:11:37
问题 I've got a prolog homework to do: There are 5 persons sitting at a round table of different nationalities (french, english, polish, italian, turkish). Each of them knows only one other language other than their own. They sit at the round table in such a way that each of them can talk with their 2 neighbors (with one neighbor they talk in their native tongue and with the other in the single foreign language they know). The english person knows italian, the polish person knows french, the

Translating a tabled predicate from b-prolog to gprolog

≯℡__Kan透↙ 提交于 2020-01-05 04:06:33
问题 For fun I've been attempting to write a Knight's Tour (https://en.wikipedia.org/wiki/Knight%27s_tour) solver in gprolog using Warnsdorf's rule. I found another SO post asking about efficiency that provided a solution in B-prolog: knight's tour efficient solution. My problem arises with the following section: :- table warnsdorff(+,+,+,+,+,-,-,min). warnsdorff(R, C, X, Y, Visits, NewX, NewY, Score) :- possible_knight_moves(R, C, X, Y, Visits, NewX, NewY), possible_moves_count(R, C, NewX, NewY,

Prolog disjunction

你说的曾经没有我的故事 提交于 2020-01-05 03:05:02
问题 Consider this Prolog predicate: silly:- 1 = 1. silly:- 1 = 2. When querying, the output answer has two states: true and then false. Is there anyway to ask Prolog to terminate as soon as it hits a true statement in a disjunction? 回答1: add a cut silly:- 1 = 1, !. silly:- 1 = 2. or use if/then/else, but then the 'program' take a very different shape, being the alternative branches merged into a single clause. Also note that, as stated in documentation Unlike !/0, the choice point of the

Splitting a list into two lists equal in length in Prolog

烈酒焚心 提交于 2020-01-04 14:22:06
问题 I am writing a predicate in Prolog to divide a list into two equal in length lists. For example : div([1,2,3,4] , X , Y). X = [1,2]. Y = [3,4]. That's my code but it's not working : div(L , L1 , L):- length(L) == length(L1). div([ H | T ] , [ H | T1] , L2):- div(T , T1 , L2). 回答1: Could be just: div(L, A, B) :- append(A, B, L), length(A, N), length(B, N). Read as "appending list A and list B results in original list L, length of A is some value N and also length of B is the same value N". 回答2

Prolog Finding middle element in List

风格不统一 提交于 2020-01-04 14:01:07
问题 I am trying to make use of prolog predicates and find middle element of a given list. My idea was to cut first and last element of list using recursion.Unfortunately I dont know how to handle recursion call properly. delete_last(L, L1) :- append(L1, [_], L). delete_first(L,L1) :- append([_],L1,L). check_len(L) :- length(L,LEN), \+ 1 is LEN. delete_both([],_):- false. delete_both([_,_],_) :- false. delete_both([X],X):- true, write('MidElement'). delete_both(L,L2) :- delete_first(LT,L2), delete