# Using maplist with many arguments: Example

**URL:** <https://swi-prolog.discourse.group/t/using-maplist-with-many-arguments-example/3369>\
**Category:** Example - Discussion\
**Created:** [December 15, 2020, 8:19am UTC](https://swi-prolog.discourse.group/t/using-maplist-with-many-arguments-example/3369 "2020-12-15T08:19:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [December 15, 2020, 8:19am UTC](https://swi-prolog.discourse.group/t/using-maplist-with-many-arguments-example/3369/1 "2020-12-15T08:19:48Z")

</div>

The current maplist implementation allows up to 4 lists. Here is what happens if you happen to have a predicate with more than 4 arguments:

```prolog
$ cat maplistexample.pl
sum_products(A, B, C, D, R) :-
    R is A+B * C+D.

sum_products_lists(As, Bs, Cs, Ds, Rs) :-
    maplist(sum_products, As, Bs, Cs, Ds, Rs).

```

This compiles but throws an error at run-time:

```prolog
?- [maplistexample].
true.

?- sum_products_lists([1,2], [3,4], [5,6], [7,8], Result).
ERROR: Unknown procedure: maplist/6
ERROR: In:
ERROR: [11] maplist(sum_products,[1,2],[3,4],[5,6],[7,8],_22184)
ERROR: [10] sum_products_lists([1,2],[3,4],[5,6],[7,8],_22264) at maplistexample.pl:5
ERROR: [9] toplevel_call(user:user: ...) at swipl/lib/swipl/boot/toplevel.pl:1113
   Exception: (11) maplist(sum_products, [1, 2], [3, 4], [5, 6], [7, 8], _11284) ? abort

```

One work-around is to use library(apply\_macros).

```prolog
:- use_module(library(apply_macros)).

sum_products(A, B, C, D, R) :-
    R is A+B * C+D.

sum_products_lists(As, Bs, Cs, Ds, Rs) :-
    maplist(sum_products, As, Bs, Cs, Ds, Rs).

```

… and then:

```prolog
?- make.
% library(apply_macros) compiled into apply_macros 0.00 sec, 53 clauses
% maplistexample compiled 0.00 sec, 3 clauses
true.

?- sum_products_lists([1,2], [3,4], [5,6], [7,8], Result).
Result = [23, 34].

```

---

<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:** [December 15, 2020, 8:51am UTC](https://swi-prolog.discourse.group/t/using-maplist-with-many-arguments-example/3369/2 "2020-12-15T08:51:54Z")

</div>

Good. Note that instead of calling you could do

```
?- list_undefined.

```

For people who want to play with this, I can grant write access to the examples github repo. I plan to add a webhook, so the changes are immediately visible on the backend servers.

If you want access, respond by direct message with your github user name. Of course, anyone can fork and make pull requests.

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [December 15, 2020, 9:09am UTC](https://swi-prolog.discourse.group/t/using-maplist-with-many-arguments-example/3369/3 "2020-12-15T09:09:28Z")

</div>

> [@jan](#):
>
> anyone can fork and make pull requests.

This sounds a good enough approach for me at least.

About `list_undefined`: Now that I read the docs, I finally understand why `?- make.` shows things that I don’t see otherwise (the reason is that make/0 runs `list_undefined/0`).
