# Reverse/2 why related to a reverse/4 in spite of a reverse/3?

**URL:** <https://swi-prolog.discourse.group/t/reverse-2-why-related-to-a-reverse-4-in-spite-of-a-reverse-3/4085>\
**Category:** Predicate\
**Created:** [June 17, 2021, 5:47am UTC](https://swi-prolog.discourse.group/t/reverse-2-why-related-to-a-reverse-4-in-spite-of-a-reverse-3/4085 "2021-06-17T05:47:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wisermans](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/wisermans/32/3113_2.png) [@Wisermans](https://swi-prolog.discourse.group/u/Wisermans)\
**Post date:** [June 17, 2021, 5:47am UTC](https://swi-prolog.discourse.group/t/reverse-2-why-related-to-a-reverse-4-in-spite-of-a-reverse-3/4085/1 "2021-06-17T05:47:29Z")

</div>

Looking at reverse/2 and its related predicates, i was wondering why it is [reverse/4](https://www.swi-prolog.org/pldoc/doc/_SWI_/library/lists.pl?show=src#reverse/2) in the SWI Prolog library in spite of [reverse/3](http://www.let.rug.nl/bos/lpn//lpnpage.php?pagetype=html&pageid=lpn-htmlse25) in Learning Prolog Now … anybody to explain it ?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 17, 2021, 6:30am UTC](https://swi-prolog.discourse.group/t/reverse-2-why-related-to-a-reverse-4-in-spite-of-a-reverse-3/4085/2 "2021-06-17T06:30:21Z")

</div>

Using lpn:

```prolog
accRev([H|T],A,R):- accRev(T,[H|A],R).
accRev([],A,A).

rev(L,R):- accRev(L,[],R).

```

```prolog
?- rev(X, [a,b]).
ERROR: Stack limit (1.0Gb) exceeded
ERROR: Stack sizes: local: 0.7Gb, global: 0.2Gb, trail: 34.1Mb

```

While

```prolog
?- reverse(X, [a,b]).
X = [b, a] ;
false.

```

🙂

---

<div class="post-metadata">

**Author:** ![Wisermans](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/wisermans/32/3113_2.png) [@Wisermans](https://swi-prolog.discourse.group/u/Wisermans)\
**Post date:** [June 17, 2021, 6:34am UTC](https://swi-prolog.discourse.group/t/reverse-2-why-related-to-a-reverse-4-in-spite-of-a-reverse-3/4085/3 "2021-06-17T06:34:27Z")

</div>

Thanks. Always simpler with a concrete example moreover i don’t know why i didn’t see at first the [mentions](https://www.swi-prolog.org/pldoc/doc_for?object=reverse/2) by LogicalCaptain. I reopened that subject as i found it interesting to understand why such a difference in code matters.

> It is tail-recursive, thus can be optimized into a loop by the compiler. Instead of keeping the elements on a stack with the depth of the list, to be collected and prepended to result list when coming back from the recursion, it constructs the result as a structure on the heap: `[X|Rs]` to be unified with the result variable Ys in the base case.\*
> 
> The _Bound_ is there to make the call symmetric, and make sure the call fails early when both lists are of unequal length or makes sure the list are of equal length when there is one open list among the arguments. But I can’t get my head around all of the special cases covered by it. Alien technology!
