prolog-cut

What does the “-” symbol mean in Prolog when dealing with lists?

喜夏-厌秋 提交于 2020-01-14 07:21:46
问题 I was reading the answer to this question, p(X) :- read(A), q(A,X-[]). q(end,X-X) :- !. q(A,[A|X]-Y) :- read(B), q(B,X-Y). The code above uses the syntax List-List . I somewhat understand what is going on, but I want to know what exactly what the "-" symbol/predicate does here. Also, is this SWI specific? 回答1: The (-)/2 to represent difference lists is a rather uncommon convention. In older books, another operator (\)/2 was used too. Many prefer to use two separate arguments instead. There

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

Use cut in Prolog to define a once_member/2 function

和自甴很熟 提交于 2019-12-30 18:26:36
问题 Disclaimer: This is informal and non-assessed coursework to do in my own time. I have tried it myself, failed and am now looking for some guidance. I am trying to implement a version of the member/2 function which will only return members for a list once. For example: | ?- member(X, [1,2,3,1]). X = 1 ? ; X = 2 ? ; X = 3 ? ; X = 1 ? ; I would like it to only print out each number a maximum of once. | ?- once_member(X, [1,2,3,1]). X = 1 ? ; X = 2 ? ; X = 3 ? ; no We have been told to do this

Prolog - differences between red cut and green cut

ぃ、小莉子 提交于 2019-12-28 14:31:26
问题 I started learning prolog, and wanted to make the whole cuts thing clearer. I have read that "green cut doesnt change declarative meaning of the program, while red cut does". But, the meaning of the program isnt really pure declarative (just from the fact that prolog actually backtracks for all options). Here is an example: p(1). p(2) :- !. p(3). it has been said that this is green cut. But if I run this: p(X), X =:= 3. I will get "true" without a cut, and "false" with a cut. so, what do I

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

Prolog append with cut operator

你离开我真会死。 提交于 2019-12-17 09:52:11
问题 What problem can occur when we use append with cut operator? append2([],L,L):-!. append2([H|T],L,[H|TL]):-append2(T,L,TL). I have tried several different inputs, but it always succeeds. ?- append2([1,2],[5],L). L = [1, 2, 5]. ?- append2([1,2],[1,2],L). L = [1, 2, 1, 2]. ?- append2([],[1,2],L). L = [1, 2]. ?- append2([1,2],[],L). L = [1, 2]. 回答1: There are two kinds of cuts; green cuts and red cuts. Green cuts are inserted just to improve efficiency and don't change the semantics of the

How does pruning choice points in the code below make it more efficient (Prolog)?

怎甘沉沦 提交于 2019-12-13 13:32:32
问题 In the code given below, there is the ! (cut) that prunes the choice point for efficiency. I am pretty certain that the reverse predicate and the agent_do_moves predicate are essential. solve_task(Task,Cost):- agent_current_position(oscar,P), solve_task_a(Task,[b(0,0,P)],[],R,Cost,_NewPos),!, % prune choice point for efficiency reverse(R,[_Init|Path]), agent_do_moves(oscar,Path). 回答1: The cut in above examples has the following effect: Ideally, it commits the search which might happen within

How to interpret this Prolog goal with a cut, and improve efficiency

…衆ロ難τιáo~ 提交于 2019-12-11 05:15:17
问题 I have been reading through the answers and comments of my previous question and I have tried applying the given explanations on an example from Bratko (Prolog Programming for Artificial Intelligence, p. 130), but I am not sure I understand it completely. The example is described below: I read the tree and the code as follows: In the goal list C :- P, Q, R, !, S, T, U. Prolog will one by one try to instantiate the variables, as normal, to eventually get to true. . Let's say that a value is

Reading a cut ! in Prolog

喜欢而已 提交于 2019-12-10 17:23:31
问题 I am reading through Learn Prolog Now! 's chapter on cuts and at the same time Bratko's Prolog Programming for Artificial Intelligence, Chapter 5: Controlling Backtracking. At first it seemed that a cut was a straight-forward way to mimic an if-else clause known from other programming languages, e.g. # Find the largest number max(X,Y,Y):- X =< Y,!. max(X,Y,X). However, as is noted down the line this code will fail in cases where all variables are instantiated even when we expect false , e.g.

Please explain the cut in the Bubblesort Prolog program?

♀尐吖头ヾ 提交于 2019-12-03 18:16:19
问题 I'm currently working trough the Bratko Prolog book and I am looking at the bubble-sort Program. I can't seem to figure out why the cut( ! ) is necessary. Say the cut isn't there, and Prolog would backtrack, how could it possibly find bad answers? Because if I leave the cut out of it, Prolog begins by giving me the correct answer but then also gives alternative bad answers. As I see it, how can swap ever return a non sorted list? And how is it possible that a non sorted list ever hits the