Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a piece of a list?

Tags:

f#

I have a list like [1..12] and I would like to get a piece like [4..9]. No clue how I can do that, I'm new with F#. I don't know if there is a built-in method for that, but I would like to know the manual way.

like image 852
Fabricio Avatar asked Dec 11 '22 12:12

Fabricio


1 Answers

[1..12] |> List.filter (fun x -> x >= 4 && x <= 9)

or

[1..12] |> Seq.skip 3 |> Seq.take 6 |> Seq.toList

Lists don't support slicing, but if you use an array instead you can also do this:

[|1..12|].[3..8]

(note 3..8 instead of 4..9 because of 0-based indexing)

like image 84
Gustavo Guerra Avatar answered Dec 22 '22 12:12

Gustavo Guerra