# Nth0/3 implementaion in swi-prolog

**URL:** <https://swi-prolog.discourse.group/t/nth0-3-implementaion-in-swi-prolog/3317>\
**Category:** Discussion\
**Created:** [November 29, 2020, 11:40am UTC](https://swi-prolog.discourse.group/t/nth0-3-implementaion-in-swi-prolog/3317 "2020-11-29T11:40:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajashekar](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@rajashekar](https://swi-prolog.discourse.group/u/rajashekar)\
**Post date:** [November 29, 2020, 11:40am UTC](https://swi-prolog.discourse.group/t/nth0-3-implementaion-in-swi-prolog/3317/1 "2020-11-29T11:40:26Z")

</div>

I came across this code, which I assume will be faster than iterating one element at a time.

```prolog
nth0_det(0, [Elem|_], Elem) :- !.
nth0_det(1, [_,Elem|_], Elem) :- !.
nth0_det(2, [_,_,Elem|_], Elem) :- !.
nth0_det(3, [_,_,_,Elem|_], Elem) :- !.
nth0_det(4, [_,_,_,_,Elem|_], Elem) :- !.
nth0_det(5, [_,_,_,_,_,Elem|_], Elem) :- !.
nth0_det(N, [_,_,_,_,_,_ |Tail], Elem) :-
    M is N - 6,
    M >= 0,
    nth0_det(M, Tail, Elem).

```

Is there any particular reason the unrolling stopped at 6 elements? Is it just chosen arbitrarily?  
Here it was first introduced [https://github.com/SWI-Prolog/swipl-devel/commit/69feecc5228b933d9800ce07e125cf889271e012](https://github.com/SWI-Prolog/swipl-devel/commit/69feecc5228b933d9800ce07e125cf889271e012)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 29, 2020, 1:14pm UTC](https://swi-prolog.discourse.group/t/nth0-3-implementaion-in-swi-prolog/3317/2 "2020-11-29T13:14:58Z")

</div>

This is just a pure guest ([WAG](https://en.wikipedia.org/wiki/Scientific_wild-ass_guess)) but sometimes they are correct.

Obviously such generators could could go on until memory is exhausted. So the [ISO standard](https://stackoverflow.com/tags/iso-prolog/info) for Prolog set the limit on the number of arguments for such predicates. I don’t think 6 is the limit because [lambdas](https://www.swi-prolog.org/pldoc/man?section=yall) use 7.

I tried to find the exact wording in the standard but searching for it was not productive. There were over a thousand hits for `6` and then trying to read each for comprehension was exhausting.

I know the concept was noted in a post on this site and that if you look for other code such as the [lambdas](https://www.swi-prolog.org/pldoc/man?section=yall) that accept a number of arguments it might put you in the ball park of where it is defined.

HTH

---

<div class="post-metadata">

**Author:** ![rajashekar](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@rajashekar](https://swi-prolog.discourse.group/u/rajashekar)\
**Post date:** [November 29, 2020, 1:39pm UTC](https://swi-prolog.discourse.group/t/nth0-3-implementaion-in-swi-prolog/3317/3 "2020-11-29T13:39:58Z")

</div>

I do not understand how the limit on number of parameters will apply in this situation.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 29, 2020, 2:38pm UTC](https://swi-prolog.discourse.group/t/nth0-3-implementaion-in-swi-prolog/3317/5 "2020-11-29T14:38:44Z")

</div>

OK I see what you are asking now. (For others the [question](https://swi-prolog.discourse.group/t/nth0-3-implementaion-in-swi-prolog/3317/3) was edited)

I agree that the limit for augments does not apply for list AFAIK, so the decision AFAIK was arbitrarily made by the creator of the code.

However in many programming languages, such limits are common and even defined in the standard.

The main reason I know of for limiting the number to less than 10 in any programming language is that **if** you need more than that you really should rethink the design of your code; most likely a change in the data structure.

---

<div class="post-metadata">

**Author:** ![rajashekar](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@rajashekar](https://swi-prolog.discourse.group/u/rajashekar)\
**Post date:** [November 29, 2020, 3:11pm UTC](https://swi-prolog.discourse.group/t/nth0-3-implementaion-in-swi-prolog/3317/9 "2020-11-29T15:11:20Z")

</div>

In [https://www.swi-prolog.org/pldoc/man?section=deep-indexing](https://www.swi-prolog.org/pldoc/man?section=deep-indexing), there is  
` Currently, the depth of indexing is limited to 7 levels.`  
A list of length more than 6 is essentially a term of depth more than 6, is this why the loop is unrolled only 6 times?
