Stuck on cut problem

From: Paul (paul_nospam_at_laberg.com.au)
Date: 02/21/04


Date: Sun, 22 Feb 2004 08:56:05 +1100

Simple program from "Programming in Prolog" (Clocksin & Mellish)

sum_to(1,1) :- !.
sum_to(N,Res) :- N1 is N - 1, sum_to(N1, Res), Res is Res + N.

The results are supposed to be:
?- sum_to(5,X).
X = 15;
no

But I get:
?- sum_to(5,X).
no

I have checked and rechecked the code, it gets to the cut and then fails on
"Res is Res + N". I am using SWI Prolog could there be a difference in the
way the cut works? To remove the possibility of the cut causing the problem
I rewrote the program as follows:

sum_to(1,1).

sum_to(N,R) :- not(N=1), N1 is N - 1, sum_to(N1, R), R is R + N.

It is still failing on "R is R + N"

Any ideas would be appreciated.



Relevant Pages

  • Re: multiset ordering
    ... can be uninstantiated when you submit a query to Prolog? ... I don't know, some design-rule to follow... ... Clocksin and Mellish, ...
    (comp.lang.prolog)
  • Newbie, *really* basic question
    ... Prolog, and got a copy of Programming in Prolog by Clocksin & Mellish. ... I realize it's telling me that there is no predicate "likes" that takes ...
    (comp.lang.prolog)
  • Re: Starting with prolog?
    ... "Where and How to start Prolog?" ... A.L. suggested: Clocksin and Mellish ... "Programming in Prolog" ...
    (comp.lang.prolog)
  • Re: problem: how to translate predicate logic sentences to Prolog?
    ... Programming in Prolog. ... Actor Prolog language, where the algorithm from the Clocksin Melish ...
    (comp.lang.prolog)
  • Re: Starting with prolog?
    ... "Where and How to start Prolog?" ... Get Clocksin and Mellish "Programming in Prolog" and read (wrong... ... DO A LOT OF EXERCISES, and the best source for these exercises is book ...
    (comp.lang.prolog)