Understand listings and recursion in Prolog

It's hard for me to think in the Prolog. What is wrong with this statement:

numberList([], 0).
numberList([H|T], Limit) :-
    H is Limit,
    numberList(T, Limit - 1).

I would like to

?- numberList(X,Limit).

to determine [Limit, Limit-1 ... 1]as the only solution for a given value for Limit, i.e.

?- numberList(X, 100).

will give X = [100, 99, 98, ..., 1]..

My guess is that, in my understanding, something is not right here because it does not work. I do not necessarily ask for a decision on what I'm trying to do, I just would like to understand why my first attempt is wrong.

+3
source share
2 answers

There are two main problems here:

1 You have problems with combining and arithmetic operations in the right places.

2- You are not validating your input enough

1- (is)/2 , , , 1 Limit. (is)/2 , . (=)/2 ().

2-, , . .

, , :

numberList([], 0).
numberList([Limit|T], Limit) :-
    NewLimit is Limit - 1,
    numberList(T, NewLimit).

, , :

numberList([], 0).
numberList([H|T], Limit) :-
    H = Limit,
    NewLimit is Limit - 1,
    numberList(T, NewLimit).

, , , , , ;.

: Prolog , , , . , Limit - 0. , NegativeInfinity. , , , . , , , Limit , 0, : . , !

+3

" - 1" C . / Prolog " , Limit-variable 1". Prolog "Limit-1" , Prolog "Limit-1" (Limit, 1),

"H - Limit",

Prolog (???), , C-. ( C , ), ( , ..)

- , . , " ", .

, , . , . , ( , )

- , , , , / ( /, " ", /, ). Visual Studio, Prolog .

C .. Commanding, Prolog .. .

0

All Articles