0

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).
user3011240
  • 87
  • 2
  • 8

2 Answers2

1
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).
Community
  • 1
  • 1
false
  • 10,264
  • 13
  • 101
  • 209
0

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.

joel76
  • 5,565
  • 1
  • 18
  • 22