user3011240
user3011240

Reputation: 97

Prolog: How do I return multiple values?

I want to take a value, such as 3 and return all from the given value to one. For example, if I passed in count(3), I would get 3,2, 1 separately. I don't want to return the values as a list. For what I wrote I tried to first return a value and then recursively call the next value to return. This however only returns once. What am I doing wrong?

 count(0,1).
 count(N,F) :-
      N1 is N-1,
      F is N-1,
      count(N1,F1).

Upvotes: 0

Views: 3953

Answers (2)

joel76
joel76

Reputation: 5605

May be something like that :

 count(N,F) :-
    N > 0,
    (   F = N
    ;   N1 is N-1,
        count(N1,F)).

You get

?- count(3,V).

V = 3 ;

V = 2 ;

V = 1 ;

false.

Upvotes: 0

false
false

Reputation: 10102

count(S0, S) :-
   closure0(\X0^X^succ(X,X0), S0, S).

using this definition and lambdas or

count(N,N).
count(N0,N) :-
   succ(N1,N0),
   count(N1,N).

or in plain ISO Prolog:

count(N,N).
count(N0,N) :-
   N0 > 0,   % or 1
   N1 is N0-1,
   count(N1,N).

Upvotes: 1

Related Questions