I need to create a single list from a list of lists using the List.map property in f#. This is what i got so far.
let lst1 = [[1;2;3;4];[5;6;7;8]]
let concat (list: 'a list list) =
let li = []
let q = List.map(fun (x: 'a list) -> x @ li ) list
li
printfn "%A" (concat lst1)
I expect the output
li = [1;2;3;4;5;6;7;8]
but get an empty list instead.
I don't get why the list is empty. I'm adding to the list li in the List.map function
I would generally use List.concat
for this purpose.
In FSI (the F# REPL):
> let lst1 = [[1;2;3;4];[5;6;7;8]];;
val lst1 : int list list = [[1; 2; 3; 4]; [5; 6; 7; 8]]
> List.concat lst1;;
val it : int list = [1; 2; 3; 4; 5; 6; 7; 8]
If you're seeking the challenge of writing this yourself, you might look into List.fold
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With