Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6769877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:14:44+00:00 2026-05-26T15:14:44+00:00

I worked all afternoon on a simple thing but cannot seem to get it

  • 0

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 as

[1, 3, 5, 7, 6, 8, 9, 0]

and want to create a matrix such as

[[1, 3],
 [5, 7],
 [6, 8],
 [9, 0]]

through a predicate

list2matrix/3 : list2matrix(List, Size_of_Rows, Matrix).

In this example used like :

list2matrix([1, 3, 5, 7, 6, 8, 9, 0], 2, Matrix).

The predicate should fail if the length of the list is not a multiple of the size of the rows.

I decided not to post my work since I think I got it so wrong that it would not help me to get correction on it ;(

Thanks by advance if you can propose any leads about how to deal with such a problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:14:44+00:00Added an answer on May 26, 2026 at 3:14 pm

    You can divide the problem in two parts.
    The first building block would be to build a row of N elements. That is to take the input list and split it in two lists, one will have exactly N elements (the row) and the other is the remaining of the input list.

    The second building block would be to build the matrix which is made of rows.

    list_to_matrix([], _, []).
    list_to_matrix(List, Size, [Row|Matrix]):-
      list_to_matrix_row(List, Size, Row, Tail),
      list_to_matrix(Tail, Size, Matrix).
    
    list_to_matrix_row(Tail, 0, [], Tail).
    list_to_matrix_row([Item|List], Size, [Item|Row], Tail):-
      NSize is Size-1,
      list_to_matrix_row(List, NSize, Row, Tail).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the beginning this worked fine: $ rake cucumber:all But then $ script/plugin install
I have a big problem with my server mysql. All worked fine but since
The code worked all along. Somehow I manage to get Visual C++ Express not
I'm very new at OCaml but worked all the past two days in order
I'm running into a strange issue.... This all worked fine last night when I
I have a workflow and am persisting it into the database. It all worked
Alright, so everything on this website was finished, it all worked, it displayed right
Afternoon All I have some data - var searchwithin = [ {id:clearwithin, type:button}, {id:search_radius,
This is the code that I made that has worked but all of a
My code worked fine when it was all in one file. Now, I'm splitting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.