0
votes

I'm trying to write predicate range\3 that takes three parameters the first is the start, the second is the end and return the generated list in the third argument.

E.g rang(1,5,L).

L = [1, 2, 3, 4, 5]

I used this code

range(E,E,[E]).

range(S,E,L):-
    S1 is S + 1,
    range(S1,E,[S|L]).

But it does not work, when i used trace command to know where is the error i recognized that the base case is useless, I also tried the green cut !in the base case but it does not work range(E,E,[E]),!.

So, if any one knows what is the problem please help me

1

1 Answers

0
votes

You're building the list in 'wrong' sense. Consider that when you'll call the base case, it will receive the consed list. How could match a single element list ? Try instead

range(S,E,[S|L]):-
    S1 is S + 1,
    range(S1,E,L).