# My notes on difflists

**URL:** <https://swi-prolog.discourse.group/t/my-notes-on-difflists/2269>\
**Category:** Nice to know\
**Created:** [May 3, 2020, 1:43pm UTC](https://swi-prolog.discourse.group/t/my-notes-on-difflists/2269 "2020-05-03T13:43:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dtonhofer](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/dtonhofer/32/902_2.png) [@dtonhofer](https://swi-prolog.discourse.group/u/dtonhofer)\
**Post date:** [May 3, 2020, 1:43pm UTC](https://swi-prolog.discourse.group/t/my-notes-on-difflists/2269/1 "2020-05-03T13:43:32Z")

</div>

I have been [Rubber-Duck Debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) about difference lists. I thought I had understood those but then no, and this cycle went on for some time.

Here is some resulting code:

- [Determine the length of a difference list](https://github.com/dtonhofer/prolog_notes/blob/34043cc36e344008e1d5123d426c0cd3dd047b9f/difflists/dl.pl)
- [Extensive code to explain append-to-difflist](https://github.com/dtonhofer/prolog_notes/blob/34043cc36e344008e1d5123d426c0cd3dd047b9f/difflists/dl.pl)

Here is some documentation: [Extensive difference list explainer](https://github.com/dtonhofer/prolog_notes/tree/master/difflists). I have actually my own graphing and vocabulary style for this now, … plus several new questions which I hope to solve eventually.

Enjoy.

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [May 3, 2020, 2:00pm UTC](https://swi-prolog.discourse.group/t/my-notes-on-difflists/2269/2 "2020-05-03T14:00:49Z")

</div>

Related, Logtalk provides a `difflist` library object, implementing the same protocol defined for the `list` object:

API documentation: [https://logtalk.org/library/difflist\_0.html](https://logtalk.org/library/difflist_0.html)  
Source code: [https://github.com/LogtalkDotOrg/logtalk3/blob/3bd198125d74191d07c35de7c0d3a86133d0668e/library/types/difflist.lgt](https://github.com/LogtalkDotOrg/logtalk3/blob/3bd198125d74191d07c35de7c0d3a86133d0668e/library/types/difflist.lgt)

It may provide some answers. Contributions welcome.

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [May 3, 2020, 3:21pm UTC](https://swi-prolog.discourse.group/t/my-notes-on-difflists/2269/4 "2020-05-03T15:21:45Z")

</div>

In your code for computing the length of a difference list, one case you’re missing is:

```prolog
?- length_dl([1,2,3]-X, Length).
false.

```

The same exception as in:

```prolog
?- length_dl(X-Y, Length).
ERROR: Unhandled exception: domain_error(difference_list(overall_structure),_1534-_1536)

```

would be expected given that you try to check if the first argument is a valid difference list. Your definition also doesn’t (yet?) support a general query such as:

```prolog
?- length_dl(Difflist, Length).
ERROR: Unhandled exception: domain_error(difference_list(overall_structure),_2296-_2298)

```

Here, you would want the similar behavior to `length/2`:

```prolog
?- difflist::length(Difflist, Length).
Difflist = _3594-_3594,
Length = 0 ;
Difflist = [_4632|_4628]-_4628,
Length = 1 ;
Difflist = [_4632, _5676|_4628]-_4628,
Length = 2 ;
Difflist = [_4632, _5676, _6720|_4628]-_4628,
Length = 3 ...

```

Keep up the good work.

---

<div class="post-metadata">

**Author:** ![dtonhofer](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/dtonhofer/32/902_2.png) [@dtonhofer](https://swi-prolog.discourse.group/u/dtonhofer)\
**Post date:** [May 7, 2020, 4:30pm UTC](https://swi-prolog.discourse.group/t/my-notes-on-difflists/2269/5 "2020-05-07T16:30:06Z")

</div>

Thanks Paulo. Your are absolutely right. Something for the TODO stack.

---

<div class="post-metadata">

**Author:** ![dtonhofer](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/dtonhofer/32/902_2.png) [@dtonhofer](https://swi-prolog.discourse.group/u/dtonhofer)\
**Post date:** [May 10, 2020, 6:43pm UTC](https://swi-prolog.discourse.group/t/my-notes-on-difflists/2269/6 "2020-05-10T18:43:25Z")

</div>

Well, I have updated the whole code.

For some reason, I took me a _long_ while to organize and find a good to think about parameter checking as I wanted to avoid ugly code.

> <https://github.com/dtonhofer/prolog_notes/blob/7869fffa5adac6dd9b71c15107f6011a67c4297e/difflists/code/README.md>
