failure-slice

Prolog finding the largest integer in a list from the tail

≯℡__Kan透↙ 提交于 2019-11-28 01:23:24
I need to find the largest integer in a list from the head of a list and alternatively from the tail. I have already written a program that can find the largest from the head now I need some help to do it from the tail. Here is what I have so far: largest([X],X). largest([X|Xs],X) :- largest(Xs,Y), X>=Y. largest([X|Xs],N) :- largest(Xs,N), N>X. Keep in mind that this finds the largest integer from the head, and I need it to work from the tail. Thanks for the help. false Hold on for a second! Before you continue, first measure the time your predicate takes! ?- length(J,I), I>10, append(J,[2],L)

Prolog predicate - infinite loop

ε祈祈猫儿з 提交于 2019-11-27 07:54:31
问题 I need to create a Prolog predicate for power of 2, with the natural numbers. Natural numbers are: 0, s(0), s(s(0)) ans so on.. For example: ?- pow2(s(0),P). P = s(s(0)); false. ?- pow2(P,s(s(0))). P = s(0); false. This is my code: times2(X,Y) :- add(X,X,Y). pow2(0,s(0)). pow2(s(N),Y) :- pow2(N,Z), times2(Z,Y). And it works perfectly with the first example, but enters an infinite loop in the second.. How can I fix this? 回答1: This happends because the of evaluation order of pow2. If you switch

Better termination for s(X)-sum

混江龙づ霸主 提交于 2019-11-27 07:48:14
问题 (Let me sneak that in within the wave of midterm questions.) A common definition for the sum of two natural numbers is nat_nat_sum/3 : nat_nat_sum(0, N, N). nat_nat_sum(s(M), N, s(O)) :- nat_nat_sum(M, N, O). Strictly speaking, this definition is too general, for we have now also success for ?- nat_nat_sum(A, B, unnatural_number). Similarly, we get the following answer substitution: ?- nat_nat_sum(0, A, B). A = B. We interpret this answer substitution as including all natural numbers and do

Prolog finding the largest integer in a list from the tail

允我心安 提交于 2019-11-26 23:31:14
问题 I need to find the largest integer in a list from the head of a list and alternatively from the tail. I have already written a program that can find the largest from the head now I need some help to do it from the tail. Here is what I have so far: largest([X],X). largest([X|Xs],X) :- largest(Xs,Y), X>=Y. largest([X|Xs],N) :- largest(Xs,N), N>X. Keep in mind that this finds the largest integer from the head, and I need it to work from the tail. Thanks for the help. 回答1: Hold on for a second!

Features of good Prolog code? [closed]

烈酒焚心 提交于 2019-11-26 16:10:23
问题 What are the design heuristics one has to master to write good Prolog? I've heard it takes an experienced programmer about two years to become proficient in Prolog. Using recursion effectively is part of it, but that seems to be a relatively minor hurdle. What exactly is it that gives programmers so much trouble? What should I be looking for in sample code to judge its quality? 回答1: The major difficulty in writing good Prolog code lies in not only understanding but also adequately

Prolog successor notation yields incomplete result and infinite loop

一曲冷凌霜 提交于 2019-11-26 11:51:47
I start to learn Prolog and first learnt about the successor notation. And this is where I find out about writing Peano axioms in Prolog. See page 12 of the PDF : sum(0, M, M). sum(s(N), M, s(K)) :- sum(N,M,K). prod(0,M,0). prod(s(N), M, P) :- prod(N,M,K), sum(K,M,P). I put the multiplication rules into Prolog. Then I do the query: ?- prod(X,Y,s(s(s(s(s(s(0))))))). Which means finding the factor of 6 basically. Here are the results. X = s(0), Y = s(s(s(s(s(s(0)))))) ? ; X = s(s(0)), Y = s(s(s(0))) ? ; X = s(s(s(0))), Y = s(s(0)) ? ; infinite loop This result has two problems: Not all results

Prolog successor notation yields incomplete result and infinite loop

放肆的年华 提交于 2019-11-26 02:37:46
问题 I start to learn Prolog and first learnt about the successor notation. And this is where I find out about writing Peano axioms in Prolog. See page 12 of the PDF: sum(0, M, M). sum(s(N), M, s(K)) :- sum(N,M,K). prod(0,M,0). prod(s(N), M, P) :- prod(N,M,K), sum(K,M,P). I put the multiplication rules into Prolog. Then I do the query: ?- prod(X,Y,s(s(s(s(s(s(0))))))). Which means finding the factor of 6 basically. Here are the results. X = s(0), Y = s(s(s(s(s(s(0)))))) ? ; X = s(s(0)), Y = s(s(s