Flatten a list of lists in Prolog
2 answers
Using a predicateappend/2
:
? - L = [[[a, b], [d, e]], [[m, f], [p, o]], [[r, l], [v, d]]], append ( L, R). L = [[[a, b], [d, e]], [[m, f], [p, o]], [[r, l], [v, d]]], R = [[a, b], [d, e], [m, f], [p, o], [r, l], [v, d]].
You should look at the implementation of SWI-Prolog and copy it if you need. Leave must_be/2
if you have to do this in GNU-Prolog.
But if you need it because of findall/3
, keep in mind that there may also be available findall/4
(not for GNU-Prolog, but it has SWI-Prolog):
$ swipl Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.2-25-gf8c39d8) Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For help, use? - help (Topic). or? - apropos (Word). ? - findall (X, between (1,3, X), Xs, Rest), findall (Y, between (7,11, Y), Rest). Xs = [1, 2, 3, 7, 8, 9, 10, 11], Rest = [7, 8, 9, 10, 11].
Just about any situation where you need to flatten a list can be avoided with diff lists.
0
user1812457
source
to share