Seq.where with Seq.groupBy

While learning F #, I do a little task:

Enter a string and the program calculates the number of vowels in the text. For additional complexity, you need to report the amount of each vowel found.

This is what I have so far:

let rec Main = 
    let vowels = [| 'a'; 'e'; 'i'; 'o'; 'u'|]
    let charIsVowel char = Seq.exists ((=) char) vowels

    let inputText = Console.ReadLine()

    let expression = 
        |> Seq.groupBy (fun char -> char)
        |> Seq.where charIsVowel


The problem is that I cannot access the grouping key, which is char

, and therefore I cannot filter the sequence. Specific error:

Type mismatch. Expecting char * seq -> bool
but given char -> bool
Type 'char * seq' does not match type 'char'

Ultimately, I want to filter the sequence of gropings according to their key. How can i do this?


source to share

1 answer

You just need to wrap the tuple first before calling charIsVowel


Seq.where (fun (char, s) -> charIsVowel char)


An alternative is to use a function fst

together with a function structure:

Seq.where (fst >> charIsVowel)


The function fst

converts (char, s)

to char

, and then the operator >>

passes the result to charIsVowel


Please note that your (fun char -> char)

in groupBy

can be simply expressed as a function of identification id


Seq.groupBy id




All Articles