tbarros
tbarros

Reputation: 43

Prolog lists transforming

I want to transform a list in this format:

C=[via(A,B,C,D),via(G,T,H,U),via(J,O,L,P)]

into the following:

F=[(C,D),(H,U),(L,P)]

The letters from F correspond to the letters from C.

Upvotes: 2

Views: 96

Answers (3)

CapelliC
CapelliC

Reputation: 60004

several Prologs (like SWI-Prolog I'm using here, in library(apply)) have maplist:

1 ?- [user].
|: transform(via(_,_,C,D),(C,D)).
(ctrl+D here)
true.

2 ?- X = [via(A,B,C,D),via(G,T,H,U),via(J,O,L,P)], maplist(transform,X,Y).
X = [via(A, B, C, D), via(G, T, H, U), via(J, O, L, P)],
Y = [ (C, D), (H, U), (L, P)].

Upvotes: 2

Ami Tavory
Ami Tavory

Reputation: 76297

It could be something like:

transform([], []).
transform([via(_, _, X, Y)|T)], [(X, Y)|TT) :-
     transform(T, TT).

Upvotes: 3

false
false

Reputation: 10102

Using library(lambda) it comes down to:

..., maplist(\via(_,_,X,Y)^(X,Y)^true, C, F), ...

Upvotes: 2

Related Questions