Extracting elements from 2d list
I have the following 2d list
[ [a1 ; a2 ; a3 ; a4] ;
[b1 ; b2 ; b3 ; b4] ;
[c1 ; c2 ; c3 ; c4] ]
and I have an input function F.
I have the following problem : Given a 2d list m and a function f, compute the i-th value in
the result column c by using the i-th value from each column in m.
[ f [a1; b1; c1]; f [a2; b2; c2]; f [a3; b3; c3]; f [a4; b4; c4]; f [a5; b5; c5] ]
How can I create a list of the first elements of each sublist? I was thinking of using the List.map function but I am not sure what function to pass to it so I can get those results.
Any help is appreciated
arrays list pattern-matching ocaml
|
show 4 more comments
I have the following 2d list
[ [a1 ; a2 ; a3 ; a4] ;
[b1 ; b2 ; b3 ; b4] ;
[c1 ; c2 ; c3 ; c4] ]
and I have an input function F.
I have the following problem : Given a 2d list m and a function f, compute the i-th value in
the result column c by using the i-th value from each column in m.
[ f [a1; b1; c1]; f [a2; b2; c2]; f [a3; b3; c3]; f [a4; b4; c4]; f [a5; b5; c5] ]
How can I create a list of the first elements of each sublist? I was thinking of using the List.map function but I am not sure what function to pass to it so I can get those results.
Any help is appreciated
arrays list pattern-matching ocaml
What have you tried? Show your code.
– Akrion
Nov 13 '18 at 20:20
@Akrion, haven't tried anything as I am not sure how to access specific elements of the sublists in OCaml
– J Doe
Nov 13 '18 at 20:21
Do you know how to get an element out of just one list?
– glennsl
Nov 13 '18 at 20:33
@glennsl yep I can pattern match on the list with hd::tl
– J Doe
Nov 13 '18 at 20:36
1
Ok, good, and so since patterns can be nested you should be able to replacehdwith another list pattern to get the elements of the sublist.
– glennsl
Nov 13 '18 at 20:39
|
show 4 more comments
I have the following 2d list
[ [a1 ; a2 ; a3 ; a4] ;
[b1 ; b2 ; b3 ; b4] ;
[c1 ; c2 ; c3 ; c4] ]
and I have an input function F.
I have the following problem : Given a 2d list m and a function f, compute the i-th value in
the result column c by using the i-th value from each column in m.
[ f [a1; b1; c1]; f [a2; b2; c2]; f [a3; b3; c3]; f [a4; b4; c4]; f [a5; b5; c5] ]
How can I create a list of the first elements of each sublist? I was thinking of using the List.map function but I am not sure what function to pass to it so I can get those results.
Any help is appreciated
arrays list pattern-matching ocaml
I have the following 2d list
[ [a1 ; a2 ; a3 ; a4] ;
[b1 ; b2 ; b3 ; b4] ;
[c1 ; c2 ; c3 ; c4] ]
and I have an input function F.
I have the following problem : Given a 2d list m and a function f, compute the i-th value in
the result column c by using the i-th value from each column in m.
[ f [a1; b1; c1]; f [a2; b2; c2]; f [a3; b3; c3]; f [a4; b4; c4]; f [a5; b5; c5] ]
How can I create a list of the first elements of each sublist? I was thinking of using the List.map function but I am not sure what function to pass to it so I can get those results.
Any help is appreciated
arrays list pattern-matching ocaml
arrays list pattern-matching ocaml
edited Nov 13 '18 at 20:19
J Doe
asked Nov 13 '18 at 20:13
J DoeJ Doe
13
13
What have you tried? Show your code.
– Akrion
Nov 13 '18 at 20:20
@Akrion, haven't tried anything as I am not sure how to access specific elements of the sublists in OCaml
– J Doe
Nov 13 '18 at 20:21
Do you know how to get an element out of just one list?
– glennsl
Nov 13 '18 at 20:33
@glennsl yep I can pattern match on the list with hd::tl
– J Doe
Nov 13 '18 at 20:36
1
Ok, good, and so since patterns can be nested you should be able to replacehdwith another list pattern to get the elements of the sublist.
– glennsl
Nov 13 '18 at 20:39
|
show 4 more comments
What have you tried? Show your code.
– Akrion
Nov 13 '18 at 20:20
@Akrion, haven't tried anything as I am not sure how to access specific elements of the sublists in OCaml
– J Doe
Nov 13 '18 at 20:21
Do you know how to get an element out of just one list?
– glennsl
Nov 13 '18 at 20:33
@glennsl yep I can pattern match on the list with hd::tl
– J Doe
Nov 13 '18 at 20:36
1
Ok, good, and so since patterns can be nested you should be able to replacehdwith another list pattern to get the elements of the sublist.
– glennsl
Nov 13 '18 at 20:39
What have you tried? Show your code.
– Akrion
Nov 13 '18 at 20:20
What have you tried? Show your code.
– Akrion
Nov 13 '18 at 20:20
@Akrion, haven't tried anything as I am not sure how to access specific elements of the sublists in OCaml
– J Doe
Nov 13 '18 at 20:21
@Akrion, haven't tried anything as I am not sure how to access specific elements of the sublists in OCaml
– J Doe
Nov 13 '18 at 20:21
Do you know how to get an element out of just one list?
– glennsl
Nov 13 '18 at 20:33
Do you know how to get an element out of just one list?
– glennsl
Nov 13 '18 at 20:33
@glennsl yep I can pattern match on the list with hd::tl
– J Doe
Nov 13 '18 at 20:36
@glennsl yep I can pattern match on the list with hd::tl
– J Doe
Nov 13 '18 at 20:36
1
1
Ok, good, and so since patterns can be nested you should be able to replace
hd with another list pattern to get the elements of the sublist.– glennsl
Nov 13 '18 at 20:39
Ok, good, and so since patterns can be nested you should be able to replace
hd with another list pattern to get the elements of the sublist.– glennsl
Nov 13 '18 at 20:39
|
show 4 more comments
2 Answers
2
active
oldest
votes
I am not sure to understand the question.
But, as I can see from your example, I would transpose your list of lists (see transpose of a list of lists)
example:
let rec transpose list = match list with
| ->
| :: xss -> transpose xss
| (x::xs) :: xss ->
(x :: List.map List.hd xss) :: transpose (xs :: List.map List.tl xss);;
//An example of int list list
let m=[ [11 ; 12 ; 13 ; 14] ;
[21 ; 22 ; 23 ; 24] ;
[31 ; 32 ; 33 ; 34] ];;
//An example of int list-> int function
let f list = List.fold_right (+) list 0;;
List.map f (transpose m);;
This gives [f [11;21;31]; f [12;22;32]; f [13;23;33]; f [14;24;34]]
add a comment |
Maybe you can try this :
open List;;
let map2d f l =
let rec aux acc l =
try (
let l'=map hd l and
l =map tl l in
aux (f l'::acc) l
) with _ -> rev acc
in
if l= then else aux l
;;
Test
let f = fold_left (+) 0;;
let id x = x;;
let m=[ [11 ; 12 ; 13 ; 14] ;
[21 ; 22 ; 23 ; 24] ;
[31 ; 32 ; 33 ; 34] ];;
# map2d id m;;
- : int list list = [[11; 21; 31]; [12; 22; 32]; [13; 23; 33]; [14; 24; 34]]
# map2d f m;;
- : int list = [63; 66; 69; 72]
# map2d id [[11;21];[12];[13]];;
- : int list list = [[11; 12; 13]]
# map2d id [;];;
- : 'a list list =
# map2d f [;];;
- : int list =
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288799%2fextracting-elements-from-2d-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I am not sure to understand the question.
But, as I can see from your example, I would transpose your list of lists (see transpose of a list of lists)
example:
let rec transpose list = match list with
| ->
| :: xss -> transpose xss
| (x::xs) :: xss ->
(x :: List.map List.hd xss) :: transpose (xs :: List.map List.tl xss);;
//An example of int list list
let m=[ [11 ; 12 ; 13 ; 14] ;
[21 ; 22 ; 23 ; 24] ;
[31 ; 32 ; 33 ; 34] ];;
//An example of int list-> int function
let f list = List.fold_right (+) list 0;;
List.map f (transpose m);;
This gives [f [11;21;31]; f [12;22;32]; f [13;23;33]; f [14;24;34]]
add a comment |
I am not sure to understand the question.
But, as I can see from your example, I would transpose your list of lists (see transpose of a list of lists)
example:
let rec transpose list = match list with
| ->
| :: xss -> transpose xss
| (x::xs) :: xss ->
(x :: List.map List.hd xss) :: transpose (xs :: List.map List.tl xss);;
//An example of int list list
let m=[ [11 ; 12 ; 13 ; 14] ;
[21 ; 22 ; 23 ; 24] ;
[31 ; 32 ; 33 ; 34] ];;
//An example of int list-> int function
let f list = List.fold_right (+) list 0;;
List.map f (transpose m);;
This gives [f [11;21;31]; f [12;22;32]; f [13;23;33]; f [14;24;34]]
add a comment |
I am not sure to understand the question.
But, as I can see from your example, I would transpose your list of lists (see transpose of a list of lists)
example:
let rec transpose list = match list with
| ->
| :: xss -> transpose xss
| (x::xs) :: xss ->
(x :: List.map List.hd xss) :: transpose (xs :: List.map List.tl xss);;
//An example of int list list
let m=[ [11 ; 12 ; 13 ; 14] ;
[21 ; 22 ; 23 ; 24] ;
[31 ; 32 ; 33 ; 34] ];;
//An example of int list-> int function
let f list = List.fold_right (+) list 0;;
List.map f (transpose m);;
This gives [f [11;21;31]; f [12;22;32]; f [13;23;33]; f [14;24;34]]
I am not sure to understand the question.
But, as I can see from your example, I would transpose your list of lists (see transpose of a list of lists)
example:
let rec transpose list = match list with
| ->
| :: xss -> transpose xss
| (x::xs) :: xss ->
(x :: List.map List.hd xss) :: transpose (xs :: List.map List.tl xss);;
//An example of int list list
let m=[ [11 ; 12 ; 13 ; 14] ;
[21 ; 22 ; 23 ; 24] ;
[31 ; 32 ; 33 ; 34] ];;
//An example of int list-> int function
let f list = List.fold_right (+) list 0;;
List.map f (transpose m);;
This gives [f [11;21;31]; f [12;22;32]; f [13;23;33]; f [14;24;34]]
answered Dec 4 '18 at 21:43
user4624500user4624500
868
868
add a comment |
add a comment |
Maybe you can try this :
open List;;
let map2d f l =
let rec aux acc l =
try (
let l'=map hd l and
l =map tl l in
aux (f l'::acc) l
) with _ -> rev acc
in
if l= then else aux l
;;
Test
let f = fold_left (+) 0;;
let id x = x;;
let m=[ [11 ; 12 ; 13 ; 14] ;
[21 ; 22 ; 23 ; 24] ;
[31 ; 32 ; 33 ; 34] ];;
# map2d id m;;
- : int list list = [[11; 21; 31]; [12; 22; 32]; [13; 23; 33]; [14; 24; 34]]
# map2d f m;;
- : int list = [63; 66; 69; 72]
# map2d id [[11;21];[12];[13]];;
- : int list list = [[11; 12; 13]]
# map2d id [;];;
- : 'a list list =
# map2d f [;];;
- : int list =
add a comment |
Maybe you can try this :
open List;;
let map2d f l =
let rec aux acc l =
try (
let l'=map hd l and
l =map tl l in
aux (f l'::acc) l
) with _ -> rev acc
in
if l= then else aux l
;;
Test
let f = fold_left (+) 0;;
let id x = x;;
let m=[ [11 ; 12 ; 13 ; 14] ;
[21 ; 22 ; 23 ; 24] ;
[31 ; 32 ; 33 ; 34] ];;
# map2d id m;;
- : int list list = [[11; 21; 31]; [12; 22; 32]; [13; 23; 33]; [14; 24; 34]]
# map2d f m;;
- : int list = [63; 66; 69; 72]
# map2d id [[11;21];[12];[13]];;
- : int list list = [[11; 12; 13]]
# map2d id [;];;
- : 'a list list =
# map2d f [;];;
- : int list =
add a comment |
Maybe you can try this :
open List;;
let map2d f l =
let rec aux acc l =
try (
let l'=map hd l and
l =map tl l in
aux (f l'::acc) l
) with _ -> rev acc
in
if l= then else aux l
;;
Test
let f = fold_left (+) 0;;
let id x = x;;
let m=[ [11 ; 12 ; 13 ; 14] ;
[21 ; 22 ; 23 ; 24] ;
[31 ; 32 ; 33 ; 34] ];;
# map2d id m;;
- : int list list = [[11; 21; 31]; [12; 22; 32]; [13; 23; 33]; [14; 24; 34]]
# map2d f m;;
- : int list = [63; 66; 69; 72]
# map2d id [[11;21];[12];[13]];;
- : int list list = [[11; 12; 13]]
# map2d id [;];;
- : 'a list list =
# map2d f [;];;
- : int list =
Maybe you can try this :
open List;;
let map2d f l =
let rec aux acc l =
try (
let l'=map hd l and
l =map tl l in
aux (f l'::acc) l
) with _ -> rev acc
in
if l= then else aux l
;;
Test
let f = fold_left (+) 0;;
let id x = x;;
let m=[ [11 ; 12 ; 13 ; 14] ;
[21 ; 22 ; 23 ; 24] ;
[31 ; 32 ; 33 ; 34] ];;
# map2d id m;;
- : int list list = [[11; 21; 31]; [12; 22; 32]; [13; 23; 33]; [14; 24; 34]]
# map2d f m;;
- : int list = [63; 66; 69; 72]
# map2d id [[11;21];[12];[13]];;
- : int list list = [[11; 12; 13]]
# map2d id [;];;
- : 'a list list =
# map2d f [;];;
- : int list =
answered Dec 9 '18 at 21:27
V. MichelV. Michel
1,418714
1,418714
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288799%2fextracting-elements-from-2d-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What have you tried? Show your code.
– Akrion
Nov 13 '18 at 20:20
@Akrion, haven't tried anything as I am not sure how to access specific elements of the sublists in OCaml
– J Doe
Nov 13 '18 at 20:21
Do you know how to get an element out of just one list?
– glennsl
Nov 13 '18 at 20:33
@glennsl yep I can pattern match on the list with hd::tl
– J Doe
Nov 13 '18 at 20:36
1
Ok, good, and so since patterns can be nested you should be able to replace
hdwith another list pattern to get the elements of the sublist.– glennsl
Nov 13 '18 at 20:39