# I know this is not a good question but

**URL:** <https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380>\
**Category:** Help!\
**Tags:** how-to\
**Created:** [December 17, 2020, 9:25am UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380 "2020-12-17T09:25:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johny23](https://avatars.discourse-cdn.com/v4/letter/j/e9a140/32.png) [@Johny23](https://swi-prolog.discourse.group/u/Johny23)\
**Post date:** [December 17, 2020, 9:25am UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/1 "2020-12-17T09:25:03Z")

</div>

How do I replace all appearances of X from a list with X+1 ? I’m very new to this.

---

<div class="post-metadata">

**Author:** ![volker-wysk](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@volker-wysk](https://swi-prolog.discourse.group/u/volker-wysk)\
**Post date:** [December 17, 2020, 10:03am UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/2 "2020-12-17T10:03:35Z")

</div>

Like this:

```prolog
repl([], []).
repl([X|Xs], [Y|Ys]) :-
    Y is X+1,   
    repl(Xs, Ys).

```

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [December 17, 2020, 7:44pm UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/3 "2020-12-17T19:44:01Z")

</div>

```prolog
% Run a Goal and print it.
test(Goal) :-
    Goal,
    writeq(Goal), nl.

% Version 1

plus_list(L0, L) :- maplist(plus1, L0, L).

plus1(X, Y) :- Y is X + 1.

?- test( plus_list([1,2,3], _Result) ).

% Version 2 (generalized to add "N" to each element)

plus_list(N, L0, L) :- maplist(plus(N), L0, L).

plus(N, X, Y) :- Y is X + N.

?- test( plus_list(2, [1,2,3], _Result) ).

% Version 3 (using "lambda")

:- use_module(library(yall)).

plus_list_(N, L0, L) :- maplist({N}/[X,Y]>>(Y is X + N), L0, L).

?- test( plus_list_(3, [1,2,3], _Result) ).

```

---

<div class="post-metadata">

**Author:** ![volker-wysk](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@volker-wysk](https://swi-prolog.discourse.group/u/volker-wysk)\
**Post date:** [December 17, 2020, 8:18pm UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/4 "2020-12-17T20:18:21Z")

</div>

That’s overkill…

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [December 17, 2020, 8:34pm UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/5 "2020-12-17T20:34:50Z")

</div>

> [@volker-wysk](#):
>
> That’s overkill…

Maybe; but it avoids a certain amount of boilerplate, and makes the code easier to understand.  
Functional programmers learn the equivalent of maplist/3, foldl/5, etc. early, and they’re equally useful for logic programming. (And if you’re worried about efficiency, you can use library(apply\_macros) instead of library(apply).)

---

<div class="post-metadata">

**Author:** ![volker-wysk](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@volker-wysk](https://swi-prolog.discourse.group/u/volker-wysk)\
**Post date:** [December 18, 2020, 2:43am UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/6 "2020-12-18T02:43:52Z")

</div>

Yes, I know. But I find it cumbersome in Prolog. In Haskell, this would be all that is needed, to define a funktion “repl” which adds one to the elements of a list:

```prolog
repl = map (+1)

```

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [December 18, 2020, 5:31am UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/7 "2020-12-18T05:31:11Z")

</div>

> [@volker-wysk](#):
>
> `repl = map (+1)`

which is equivalent to (with different parameter ordering):

```prolog
repl n l = map (\x -> x + n) l

```

is a bit more readable, especially if you want a different parameter ordering (and not all functional languages have currying). Logic programming languages aren’t as compact with functions (because they have to use predicate notation) and it’s a design choice as to whether or not be explicit about the “outer” values:

```prolog
repl(N,L0,L) :- maplist({N}/[X,Y]>>(Y is X+N),L0,L).

```

---

<div class="post-metadata">

**Author:** ![volker-wysk](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@volker-wysk](https://swi-prolog.discourse.group/u/volker-wysk)\
**Post date:** [December 18, 2020, 6:50am UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/8 "2020-12-18T06:50:34Z")

</div>

That “repl n l = …” of yours, coulde be written this way:

```prolog
repl n = map (+n)

```

As far as I’m concerned, I find this shorter notation easier to read.

However, I don’t know this yet:

```prolog
repl(N,L0,L) :- maplist({N}/[X,Y]>>(Y is X+N),L0,L).

```

Looks like it is more concise and readable, but I still find it messy. I’ts nothing compared to the elegant Haskell syntax.

Bye

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [December 18, 2020, 8:32pm UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/10 "2020-12-18T20:32:26Z")

</div>

> [@volker-wysk](#):
>
> `repl n = map (+n)`

When I read a Haskell definition like this, I mentally add arguments so that it’s equivalent to

```haskell
repl n l = map (\x -> x+n) l

```

and in general, I find point-free notation difficult to understand; so for me, the explicit "lambda"s provided by library(yall) are easier to understand, even if more verbose. I wonder if I would change my mind if I did more Haskell programming …

My guess is that most people have my difficult in understanding point-free style because Haskell provides list comprehensions (which only saves a few keystrokes in this case, but is nicer when destructuring is involved):

```haskell
repl n l = [x+n | x <- l]

```

Prolog only provides the maplist/3 form (or setof/3 et al, if using a generator). I think I saw a discussion about list comprehensions in Prolog, but can’t remember where …

---

<div class="post-metadata">

**Author:** ![volker-wysk](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@volker-wysk](https://swi-prolog.discourse.group/u/volker-wysk)\
**Post date:** [December 18, 2020, 9:11pm UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/11 "2020-12-18T21:11:06Z")

</div>

Hi!

When reading that " `repl n = map (+n)`", I _don’t_ mentally add the “missing” arguments. It’s another way of perceiving the same thing. I actually find it easier to understand in the compact way.

Well possible that you would change your mind, if you did more Haskell programming.

---

<div class="post-metadata">

**Author:** ![volker-wysk](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@volker-wysk](https://swi-prolog.discourse.group/u/volker-wysk)\
**Post date:** [December 18, 2020, 9:18pm UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/12 "2020-12-18T21:18:43Z")

</div>

Neat! I wasn’t aware of that plus/3 thing. Thanks!

Cheers,  
Volker

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [December 18, 2020, 10:39pm UTC](https://swi-prolog.discourse.group/t/i-know-this-is-not-a-good-question-but/3380/13 "2020-12-18T22:39:22Z")

</div>

I wonder if there’s an equivalent of “point-free” notation for logic programming … composition of predicates seems to require variables - or does it? (And this also leads to the clunkiness of evaluating functions in Prolog, such as

```prolog
count([], 0).
count([_|Xs], Count) :- count(Xs, Count0), Count is Count + 1.

```

compared to

```prolog
count [] = 0
count (_:xs) = 1 + count xs

```

And, yes, in both Prolog and Haskell it’s better to use accumulators or `foldl`:

```prolog
count_list(List, Count) :- foldl([_,C0,C]>>(C is C0+1), List, 0, Count).

```

```prolog
count_list = foldl (\ _ c0 -> c0+1) 0

```

Maybe something based on the notation in [Yedalog](https://research.google/pubs/pub43462/) could solve this …
