What is the easiest way to count the number of elements in a sequence in F#?
Asked
Active
Viewed 1.1k times
4 Answers
54
Use Seq.length
Returns the length of the sequence
-
3duh... all along I was looking for Count o r Total Elements! Thanks Yin – Mark Pearl Jul 06 '10 at 18:27
8
open System.Linq
mySeq.Count()

Mark H
- 13,797
- 4
- 31
- 45
-
One advantage of this solution is that you can filter inside a Count(). For example, `x |> Seq.filter (System.Char.IsLetter) |> Seq.length` could be write `(x : string).Count (fun x -> Char.IsLetter(x))` – aloisdg Jan 27 '17 at 11:50
-
6
FYI, if you search the library docs for Seq for "-> int
" you'll find this rather quickly.

Brian
- 117,631
- 17
- 236
- 300