prolog

Defining “let expressions” in Prolog

半腔热情 提交于 2020-12-30 17:19:37
问题 In many functional programming languages, it is possible to "redefine" local variables using a let expression: let example = let a = 1 in let a = a+1 in a + 1 I couldn't find a built-in Prolog predicate for this purpose, so I tried to define a let expression in this way: :- initialization(main). :- set_prolog_flag(double_quotes, chars). replace(Subterm0, Subterm, Term0, Term) :- ( Term0 == Subterm0 -> Term = Subterm ; var(Term0) -> Term = Term0 ; Term0 =.. [F|Args0], maplist(replace(Subterm0

Defining “let expressions” in Prolog

本秂侑毒 提交于 2020-12-30 17:17:36
问题 In many functional programming languages, it is possible to "redefine" local variables using a let expression: let example = let a = 1 in let a = a+1 in a + 1 I couldn't find a built-in Prolog predicate for this purpose, so I tried to define a let expression in this way: :- initialization(main). :- set_prolog_flag(double_quotes, chars). replace(Subterm0, Subterm, Term0, Term) :- ( Term0 == Subterm0 -> Term = Subterm ; var(Term0) -> Term = Term0 ; Term0 =.. [F|Args0], maplist(replace(Subterm0

How can Prolog derive nonsense results such as 3 < 2?

心已入冬 提交于 2020-12-26 07:13:42
问题 A paper I'm reading says the following: Plaisted [3] showed that it is possible to write formally correct PROLOG programs using first-order predicate-calculus semantics and yet derive nonsense results such as 3 < 2. It is referring to the fact that Prologs didn't use the occurs check back then (the 1980s). Unfortunately, the paper it cites is behind a paywall. I'd still like to see an example such as this. Intuitively, it feels like the omission of the occurs check just expands the universe

How to list all the trips discarding one airline

落花浮王杯 提交于 2020-12-15 07:22:00
问题 This is my flights data #flight(city1, city2,airline,distance,time,price). flight(dublin, london,ab,8000,4,1000). flight(moscow,london,ab,9000,5,2000). flight(dublin,moscow,bc,1000,6,3000). flight(cork, moscow,ca,2000,7,4000). flight(chicago, dublin,ab,6000,8,4500). flight(berlin, moscow,bc,3000,9,4600). flight(cork, newyork,ca,4000,10,4700). flight(paris, hongkong,bc,11000,11,4890). connected(X,Y,_,_,_,_) :- flight(X,Y,_,_,_,_) ; flight(Y,X,_,_,_,_). I have to get all the trips between city1

How to list all the trips discarding one airline

时光怂恿深爱的人放手 提交于 2020-12-15 07:20:09
问题 This is my flights data #flight(city1, city2,airline,distance,time,price). flight(dublin, london,ab,8000,4,1000). flight(moscow,london,ab,9000,5,2000). flight(dublin,moscow,bc,1000,6,3000). flight(cork, moscow,ca,2000,7,4000). flight(chicago, dublin,ab,6000,8,4500). flight(berlin, moscow,bc,3000,9,4600). flight(cork, newyork,ca,4000,10,4700). flight(paris, hongkong,bc,11000,11,4890). connected(X,Y,_,_,_,_) :- flight(X,Y,_,_,_,_) ; flight(Y,X,_,_,_,_). I have to get all the trips between city1

Eliminate consecutive duplicates of list elements with prolog

你离开我真会死。 提交于 2020-12-15 05:23:07
问题 The original problem was to come up with a way to get the following remove([a,a,a,b,q,q,q,q,e,e,e]),X) X = [a,b,q,e] 回答1: We can solve this problem by one iteration along the list. At any point in the list we check the current element and the next element, if they are the same then we ignore the current element, else if they are different we take the current element. rm_dup([], []). rm_dup([X], [X]). rm_dup([X1, X2 | Xs], [X1 | Ys]) :- dif(X1, X2), rm_dup([X2|Xs], Ys). rm_dup([X, X | Xs], Ys)

Eliminate consecutive duplicates of list elements with prolog

╄→尐↘猪︶ㄣ 提交于 2020-12-15 05:22:10
问题 The original problem was to come up with a way to get the following remove([a,a,a,b,q,q,q,q,e,e,e]),X) X = [a,b,q,e] 回答1: We can solve this problem by one iteration along the list. At any point in the list we check the current element and the next element, if they are the same then we ignore the current element, else if they are different we take the current element. rm_dup([], []). rm_dup([X], [X]). rm_dup([X1, X2 | Xs], [X1 | Ys]) :- dif(X1, X2), rm_dup([X2|Xs], Ys). rm_dup([X, X | Xs], Ys)

Prolog: Murder Mystery

做~自己de王妃 提交于 2020-12-15 01:59:28
问题 I found the following murder mystery puzzle. Since I don't know much about Prolog except the basics, I cannot really evaluate the solution, however, it didn't seem particularly nice to me. My solution is not enough to generate the correct answers so I'm looking for some pointers as to how to get there or if it's at all possible to get there with my approach. Here's the puzzle just in case the link goes down: jean was killed on Tuesday; the only suspects are: Luc, Paul, Alain, Bernard and

Prolog: Murder Mystery

狂风中的少年 提交于 2020-12-15 01:55:15
问题 I found the following murder mystery puzzle. Since I don't know much about Prolog except the basics, I cannot really evaluate the solution, however, it didn't seem particularly nice to me. My solution is not enough to generate the correct answers so I'm looking for some pointers as to how to get there or if it's at all possible to get there with my approach. Here's the puzzle just in case the link goes down: jean was killed on Tuesday; the only suspects are: Luc, Paul, Alain, Bernard and

Prolog: Murder Mystery

时光毁灭记忆、已成空白 提交于 2020-12-15 01:53:34
问题 I found the following murder mystery puzzle. Since I don't know much about Prolog except the basics, I cannot really evaluate the solution, however, it didn't seem particularly nice to me. My solution is not enough to generate the correct answers so I'm looking for some pointers as to how to get there or if it's at all possible to get there with my approach. Here's the puzzle just in case the link goes down: jean was killed on Tuesday; the only suspects are: Luc, Paul, Alain, Bernard and