简体   繁体   中英

Prolog - Matrix to list

There was a question to turn a list (of any length) to a matrix here (see link at the bottom), but I'd like to do the opposite, turn a matrix into a list, recursively.

Matrix = [[a,b,c],[d,e,f]]

define predicate :

matrixToList(MyMatrix,NewList)

where NewList = [a,b,c,d,e,f].

Can anyone please help?
Thank you.

Turn a list into a matrix

This is my code for the same problem, but yes, you can also use flatten

getAllElements([],[]).
getAllElements([H|T], ElementsList) :- getAllElements(T, NewElementsList),
                                       append(NewElementsList, H, ElementsList).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM