prolog

Displaying all the members of database in Prolog

时光毁灭记忆、已成空白 提交于 2020-02-06 08:05:09
问题 I'm trying to make a predicate "find_recipe" which fetches a list of ingredients based on the name of the recipe, given by the user. I defined 3 recipes and am trying to first show a prompt for a user, let the user enter the recipe name, and then next few lines which are commented out should handle fetching the designated recipe, and lastly the line beginning with format will print out the result. recipe('Makaronilaatikko', ['macaroni', 'potato', 'onion', 'cheese', 'milk', 'egg', 'minced meat

Displaying all the members of database in Prolog

谁说胖子不能爱 提交于 2020-02-06 08:04:15
问题 I'm trying to make a predicate "find_recipe" which fetches a list of ingredients based on the name of the recipe, given by the user. I defined 3 recipes and am trying to first show a prompt for a user, let the user enter the recipe name, and then next few lines which are commented out should handle fetching the designated recipe, and lastly the line beginning with format will print out the result. recipe('Makaronilaatikko', ['macaroni', 'potato', 'onion', 'cheese', 'milk', 'egg', 'minced meat

Prolog Assigning integer to a variable

穿精又带淫゛_ 提交于 2020-02-05 08:38:35
问题 I'm new to Prolog, and using GNU Prolog, so no clp(fd) allowed. What I'm trying to do is for a given integer N, generate a list with elements of 1 ~ N. So set(3,T). will output T = [1,2,3]. Here is what I have so far: set(0,[]). set(N,T):-set(N-1,T1),append(T1,[N],T). When I try set(2,T), it crashes. I debugged with trace, and find out that it's not evaluating N-1, but rather doing N-1-1-1... Anyone can tell me how to solve this? Thank you. 回答1: It should be: set(N,T):- N2 is N-1, set(N2,T1),

How to compile picoProlog from source code?

陌路散爱 提交于 2020-02-04 04:12:49
问题 I am a student in Computer Science, and I am learning about logic programming with Prolog. I have found an interesting Prolog interpreter, picoProlog (http://spivey.oriel.ox.ac.uk/corner/Logic_Programming). To know more about Prolog, I am trying to compile their source code, but I failed. In this web page, they said: The interpreter source is written in a minimal dialect of Pascal, avoiding many features including pointers, but using macros to overcome some of Pascal's limitations, in a style

Getting connected components of graph in Prolog

早过忘川 提交于 2020-02-03 18:59:29
问题 I'm struggling with logic programming. I have a this problem and I hope some of you can help me with it. Discontinous graph is represented by facts in this way: h(0,1). h(1,2). h(3,4). h(3,5). So there is two separate graph components. I would like all the separate components on the output represented by a list. So if there is three separate components in the graph, there will be three lists. For the given example above, the expected output is [[0,1,2],[3,4,5]] . 回答1: Using iwhen/2 we can

Check if a list is made up of N instances of X (repeat X N times)

心已入冬 提交于 2020-01-30 08:58:09
问题 Given a query such as: containsN(4,2,Z). I should get: Z = [2,2,2,2]. or containsN(4,W,[3,3,3,3]) I should get: W = 3. So in other words, for the first example I need 4 instances of 2 in a list bound to Z. For the second example I need the element in the list applied 4 times bound to W. My attempt so far results in an infinite loop: containsN(Y,W,Z) :- contains_helper(Y,W,Z). contains_helper(0,_,_). contains_helper(Y,W,[H|T]) :- W = H, Y0 is Y - 1, contains_helper(Y0,W,T). The idea is, I call

Check if a list is made up of N instances of X (repeat X N times)

余生颓废 提交于 2020-01-30 08:58:04
问题 Given a query such as: containsN(4,2,Z). I should get: Z = [2,2,2,2]. or containsN(4,W,[3,3,3,3]) I should get: W = 3. So in other words, for the first example I need 4 instances of 2 in a list bound to Z. For the second example I need the element in the list applied 4 times bound to W. My attempt so far results in an infinite loop: containsN(Y,W,Z) :- contains_helper(Y,W,Z). contains_helper(0,_,_). contains_helper(Y,W,[H|T]) :- W = H, Y0 is Y - 1, contains_helper(Y0,W,T). The idea is, I call

Prolog domino game

梦想与她 提交于 2020-01-29 09:51:53
问题 i'm making a game in prolog, with a given set of domino pieces, it should make a correct domino row using all the pieces in the initial set. we must use an inference system in which we must build the initial state and the final state like this: initial(dominos([[1,4],[2,3],[4,2]],[])). final(dominos([],[[1,4],[4,2],[2,3]])). the first transtion is just pick one pice from the 1st list and put it into the 2nd list, but the next ones should verify if the 2nd number matches the 1st number of the

Prolog domino game

谁说我不能喝 提交于 2020-01-29 09:51:12
问题 i'm making a game in prolog, with a given set of domino pieces, it should make a correct domino row using all the pieces in the initial set. we must use an inference system in which we must build the initial state and the final state like this: initial(dominos([[1,4],[2,3],[4,2]],[])). final(dominos([],[[1,4],[4,2],[2,3]])). the first transtion is just pick one pice from the 1st list and put it into the 2nd list, but the next ones should verify if the 2nd number matches the 1st number of the

Prolog permutation predicate using insertion of elements

China☆狼群 提交于 2020-01-25 21:46:24
问题 I am trying to write a predicate permutation/2 so that it is true if and only if both arguments are list, one a permutation of the other. To do this, I've written the two helper predicates delete/3 and insert/3 . The first is true if and only if the third argument is the second argument, both lists, with the first instance of the element in the first argument removed. The second is true if and only if the third argument equals the second argument with the first argument (element) inserted.