Re: newbie: Out of local stack, matching values from 0 to 10



On 2005-05-23, Matthijs Wensveen <mrw@xxxxxxxxxx> wrote:
> Hi,
> I recently started programmin in prolog again, just for fun. But
> immediately I encountered some major problems.
> I am trying to have prolog match any value from 0 to 10.
> What I have written so far:
> ---
> in_bounds(0).
>
> in_bounds(X) :-
> in_bounds(Y),
> X is Y + 1,
> X < 10.
> ---
>
> When I query in_bounds(X). all the values from 0 to and including 9 are
> matched, but then it says "ERROR: Out of local stack".
> I think I understand why, but I can't think of another way to do this.

A Prolog system with tabling like XSB will (as far as I understand it)
fix this. (but not for in_bounds(-1), right?)

> Actually I do:
>
> in_bounds(X) :- member(X, [0,1,2,3,4,5,6,7,8,9]).
>
> But this seems rather tedious.

A bit easier is

in_bounds(X) :-
between(0, 9, X).

> Secondly, is there a shorthand for lists like this? I would like to be
> able to write [1..10] or [1,..,10] or so.

You can generate lists for integers in a range using the numlist/3
library predicate.

Depending on the problem, you may wish to have a look at the constraint
library 'clp/bounds'.

Cheers --- Jan
.



Relevant Pages

  • Re: This Logo program in Prolog?
    ... Term is the basic data structure of Prolog, ... easily use a compound term for building many of the other data ... including lists (lists are just compound terms ... only other language I've seen with such love for the list type is ...
    (comp.lang.prolog)
  • Re: This Logo program in Prolog?
    ... Term is the basic data structure of Prolog, ... easily use a compound term for building many of the other data ... including lists (lists are just compound terms ... only other language I've seen with such love for the list type is ...
    (comp.lang.prolog)
  • Re: This Logo program in Prolog?
    ... I am such a beginner to Prolog that I did not think of using member ... Make more constructs/predicates part of the language or not? ... will iterate over the list of elements, ... I hate lists. ...
    (comp.lang.prolog)
  • Re: compare element in a list prolog
    ... exercises using lists, but not give them the very basic vocabulary of lists. ... If someone has gone to the bother of explaining Prolog to the point that the ... > suggest is that that skill is the essence of Prolog programming and, ... novices *do* find it difficult. ...
    (comp.lang.prolog)
  • Re: can we OR the clauses for satisfying the rule??
    ... > I am a network programer trying prolog for leisure and analysing its ... > use in network scenerios. ... > returns true if router X occurs in the route from the origin router to ... to think in terms of "lists of lists", ...
    (comp.lang.prolog)