List splitting

19 views
Skip to first unread message

James

unread,
Apr 28, 2016, 2:02:52 PM4/28/16
to SWI-Prolog
Hello,

I would like to write a predicate that can alternatively divide elements in brackets into sub lists.

list_split(L,L1,L2).

If I have this list: [(x,4),(y,6),(u,4)].

and call the predicate thus:
list_split([(x,4),(y,6),(u,4)],L1,L2).

It will return
L1=[x,y,u]
L2=[4,6,4]


It sounds really simple, but I have been trying to get something working to no avail!

Thank you very much

James

Kuniaki Mukai

unread,
Apr 28, 2016, 3:17:12 PM4/28/16
to James, SWI-Prolog
Hi James,

Your list_split/3 sounds like the zip/3 predicate which I usually define
like this:

zip([], [], []).
zip([X|Xs], [Y|Ys], [X-Y|Zs]):- zip(Xs, Ys, Zs).

I hope this help your solution.

Kuniaki Mukai
> --
> You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+...@googlegroups.com.
> Visit this group at https://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.

Jan Wielemaker

unread,
Apr 29, 2016, 2:49:46 AM4/29/16
to James, SWI-Prolog
Looks like a homework problem, but might not be. First of all, there are
terms (x,4). Although such terms are valid in Prolog, they are not commonly
used. That is mostly because it is easy to get them misinterpreted for
term arguments. For example, (x,4) is in fact ','(x,4), but -(x,4) is not
-(','(x,4)) but indeed '-'(x,4), while - (x,4) is -(','(x,4)).

The normal general purpose binary relation is typically represented as
-(left,right) and as - is an infix operator, it can be written as
left-right.

Now, there is library(pairs) to do all sorts of useful things with pairs.
See http://www.swi-prolog.org/pldoc/doc_for?object=pairs_keys_values/3

Cheers --- Jan

>
> Thank you very much
>
> James
>
> --
> You received this message because you are subscribed to the Google
> Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swi-prolog+...@googlegroups.com
> <mailto:swi-prolog+...@googlegroups.com>.

James

unread,
Apr 29, 2016, 5:22:22 AM4/29/16
to SWI-Prolog

Thanks a lot Jan,

That was a really helpful link indeed and I am reading into that now

Best wishes James
Reply all
Reply to author
Forward
0 new messages