logic-programming

Prolog: How can I implement the sum of squares of two largest numbers out of three?

痞子三分冷 提交于 2020-01-15 06:17:08
问题 Exercise 1.3 of the book Structure and Interpretation of Computer Programs asks the following: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. I'm learning Prolog. Here's the function I tried to implement: square(X, Y) :- Y is X * X. squareTwoLargest(X, Y, Z, R) :- R is square(L1) + square(L2), L1 = max(X, Y), L2 = max(min(X, Y), Z). However, when I run it, it gives the following error: ERROR: is/2: Arguments are not

Need help understanding Prolog append/3 and inverse/2 and trace output

萝らか妹 提交于 2020-01-06 06:09:30
问题 This is the question find value A. inverse([],[]). inverse([H|T],D) :- inverse(T,Z), append(Z,[H],D). append([],X,X). append([X|L],M,[X|N]) :- append(L,M,N). This is the answer: Plese help me to understand this! 回答1: The images of the Prolog code you posted show some unusual or very old Prolog, in particular for list the use of [H:T] is now done as [H|T] , notice the change from : to | , and <= is more common as :- . To understand the Prolog code it is easier to start from the bottom up. I

Is the Prova implemented with a Prolog compiler or Prolog interpreter?

五迷三道 提交于 2020-01-06 03:50:51
问题 I am looking at the Java-written Prolog system, Prova. https://prova.ws/ But it is not clear about its implementation, a Prolog compiler or Prolog interpreter? I read the manual, but did not found an answer. 回答1: There are some rumors that Prova is based on Mandarax. The newest version seem to be heading in the same direction as SWI-Prolog 7, i.e. it supports dicts and a dot notation. See also here: http://prova.ws/confluence/display/REWRITEDEV/Prova+maps+for+defining+slotted+terms The

Swapping consecutive items of a list in Prolog

一世执手 提交于 2019-12-24 02:21:26
问题 I'm trying to write Prolog code that can swap two elements of a list, but only if they are consecutive to each other . That is, conseq_swap(d, e, [a, g, d, e, f], X). should give: X = [a, g, e, d, f]. (d and e are consecutive.) However, conseq_swap(a, e, [a, g, d, e, f], X). should always fail (a and e are not consecutive.) I can assume that an item appears in the list only once. I have the following code, which is actually working fine: swap_conseq(X, Y, MainList, SwappedList) :- indexOf

Why cant i get the answer to the zebra puzzle in prolog?

筅森魡賤 提交于 2019-12-23 22:13:43
问题 this is my code currently, I am trying to solve the zebra puzzle. exists(A,(A,_,_,_,_)). exists(A,(_,A,_,_,_)). exists(A,(_,_,A,_,_)). exists(A,(_,_,_,A,_)). exists(A,(_,_,_,_,A)). rightOf(A,B,(B,A,_,_,_)). rightOf(A,B,(_,B,A,_,_)). rightOf(A,B,(_,_,B,A,_)). rightOf(A,B,(_,_,_,B,A)). middleHouse(A,(_,_,A,_,_)). firstHouse(A,(A,_,_,_,_)). nextTo(A,B,(B,A,_,_,_)). nextTo(A,B,(_,B,A,_,_)). nextTo(A,B,(_,_,B,A,_)). nextTo(A,B,(_,_,_,B,A)). nextTo(A,B,(A,B,_,_,_)). nextTo(A,B,(_,A,B,_,_)). nextTo

Most useful and instructive functional-logic language to learn

 ̄綄美尐妖づ 提交于 2019-12-20 19:43:14
问题 I was pretty amazed by the power of Prolog. It took some time to get the head around, but to me it seemed to be the coolest declarative language out there. That's why recently, after two years of some functional programming with Scala, I decided to take a look at logical programming again, to "train my brain" or better for actual usage. Combining functional and logical programming seems attractive for me to learn/solidify concepts of both declarative paradigms. I find also find strong type

Relational/Logic Programming in Python?

青春壹個敷衍的年華 提交于 2019-12-18 10:11:54
问题 I'm a longtime python developer and recently have been introduced to Prolog. I love the concept of using relationship rules for certain kinds of tasks, and would like to add this to my repertoire. Are there any good libraries for logic programming in Python? I've done some searching on Google but only found the following: jtauber's blog series on relational_python Would love to compare to some others...thanks! -aj 回答1: Perhaps you should google "Logic Programming in Python". Pyke looks

Purity of Prolog predicates that use impure primitives

家住魔仙堡 提交于 2019-12-18 03:38:29
问题 I know that var/1 , nonvar/1 and !/0 are impure primitives, but does their use make every program that uses them impure? I wrote the following predicate plus/3 that behaves as if it were pure or at least that is what I claim. The predicate is demonstrative, not designed to be efficient. % nat(X) is true if X is a natural number nat(0). nat(X):- nonvar(X), !, X > 0. nat(X):- nat(X1), X is X1 + 1. % plus(A, B, C) is true if A,B and C are natural numbers and A+B=C plus(A, B, C):- nat(A), (nonvar

Transforming recursion into tail recursion?

妖精的绣舞 提交于 2019-12-10 19:17:53
问题 I am trying to write a predicate that recursively finds the nth power of some number [A^n = A * A^(n-1)] and uses the shortcut A^(2n) = A^n * A^n. Here is the solution so far. p(_,0,1):-!. p(A,N,R):-N mod 2=0,!,N1=N/2,p(A,N1,R1),R=R1*R1. p(A,N,R):-N1=N-1,p(A,N1,R1),R=R1*A. Now I want to make this tail recursive. I can do tail for simple cases, such as factorials and power without the shortcut (by adding an accumulator), but this one is hard. Any help is much appreciated! 回答1: It seems it is

Multithreading in… functional languages? (Prolog)

﹥>﹥吖頭↗ 提交于 2019-12-06 04:36:14
问题 When my friend started learning Prolog in school, I made fun of him for learning a useless language. However, he's showed me some stuff I never even knew possible; I want to know where this technique comes from. The technique is this: permutation(List) :- isAMember(X, List), deleteFirstElement(X, List, Substring), % and so on In this code, isAMember(X, List) is a function that returns true if X is in List . However, up to this point X is not defined as a variable - so the program will spawn a