Turn a list into a matrix

后端 未结 3 653
半阙折子戏
半阙折子戏 2021-01-25 18:42

I worked all afternoon on a simple thing but cannot seem to get it right for some reason : how to turn a list into a matrix of given width.

Example : I got a list such a

3条回答
  •  花落未央
    2021-01-25 19:28

    BTW, I thought I'd mention the code I finally wrote :

    length_(Length, List) :- length(List, Length).
    
    list2matrix(List, RowSize, Matrix) :-
        length(List, L),
        HowManyRows is L div RowSize,
        length(Matrix, HowManyRows),
        maplist(length_(RowSize), Matrix),
        append(Matrix, List).
    

    It's more high order oriented and funnier to read I guess :)

提交回复
热议问题