Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the first elements in a list of Lists [F#]

I'm currently interested in F# as it is different to everything I have used before. I need to access the first element of each list contained within a large list. If I assume that the main list contains 'x' amount of lists that themselves contain 5 elements, what would be the easiest way to access each of the first elements.

let listOfLists = [ [1;2;3;4;5]; [6;7;8;9;10]; [11;12;13;14;15] ]

My desired output would be a new list containing [1;6;11]

Currently I have

let rec firstElements list  =
    match list with
    | list[head::tail] -> 
        match head with
        | [head::tail] -> 1st @ head then firstElements tail

To then expand on that, how would I then get all of the second elements? Would it be best to create new lists without the first elements (by removing them using a similar function) and then reusing this same function?

like image 828
Code Guy Avatar asked Mar 20 '18 00:03

Code Guy


People also ask

How do you take the first element of a list?

Method 1: Remove Elements From Lists in Python using pop() This pop() method, i.e removes and prints the ith element from the list.

How do you get the first element from each nested list of a list?

Approach #2 : Using zip and unpacking(*) operator This method uses zip with * or unpacking operator which passes all the items inside the 'lst' as arguments to zip function. Thus, all the first element will become the first tuple of the zipped list. Returning the 0th element will thus, solve the purpose.

How do you get the first element of a list in Python?

To access the first element (12) of a list, we can use the subscript syntax [ ] by passing an index 0 . In Python lists are zero-indexed, so the first element is available at index 0 . Similarly, we can also use the slicing syntax [:1] to get the first element of a list in Python.

How do you get all the first element of a list in R?

To extract only first element from a list, we can use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command sapply(LIST,"[[",1).


2 Answers

You can use map to extract the head element of each child list:

let firstElements li =
  match li with [] -> None | h::_ -> Some h

let myfirstElements = List.map firstElements listOfLists

I'm using Ocaml's speak with little lookup on F# so this may be inaccurate, but the idea applies.

EDIT: You can also use List.head which makes it more concise and will return a int list instead of int option list. However, it throws an exception if you hits an empty list. Most of the time, I'd avoid using List.head or List.tail in this case.

like image 192
Pandemonium Avatar answered Oct 20 '22 17:10

Pandemonium


The easisest way to access the first element in a list is List.head. As you have a list of lists, you just need to List.map this function:

let listOfLists = [ [1;2;3;4;5]; [6;7;8;9;10]; [11;12;13;14;15] ]

listOfLists 
|> List.map List.head
//val it : int list = [1; 6; 11]

Now if you need to access other elements, you can use List.item or just index into your list with xs.[1]. But please keep in mind, that for large lists this will be inefficient and if you want fast look up use an array.

listOfLists
|> List.map (List.item 1)
//val it : int list = [2; 7; 12]

With indexing:

listOfLists
|> List.map (fun x -> x.[1])
like image 23
s952163 Avatar answered Oct 20 '22 18:10

s952163