简体   繁体   中英

Combining elements of a sublist of a list

I have the following list of sublists

[[1;5;10];
 [2;6;11];
 [3;7;12]];

I am trying to a create the following list of sublists:

[[1;2;3];
 [5;6;7];
 [10;11;12]]

The first sublist of the result should containt the first element of each original sublist, second result sublist should contian the second elements of each of the original sublists and so on.

Each sublist contains the same number of elements as the other sublists. The amount of sublists is at least 2.

I was thinking of using List.map but I am not sure what function to apply to each sublist to exctract the needed elements.

This is what I have so far:

let rec compute list = 
   List.map (fun x -> ) list

Any suggestions are appreciated!

Here you need two recursions (as you would need 2 imbricated loops in an imperative language). The first recursion should allow you to go through the inputs line, say from 1 to 3, and at each step of this recursion, you will need a second recursion,to go along the full row.

You can either do it all by hand or you can use List.fold_left . (I would use fold for the inner recursion.

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