Aftershock
Aftershock

Reputation: 5351

This F# code is not working

This is not working... I get error FS0001: The type 'string' is not compatible with the type 'seq' for the last line. Why?

let rec Parse (charlist) =
   match charlist with
   | head :: tail -> printf "%s " head
                     Parse tail
   | [] -> None

Parse (Seq.toList "this is a sentence.") |> ignore

Upvotes: 0

Views: 250

Answers (1)

kvb
kvb

Reputation: 55184

The problem is that printf "%s " head means that head must be a string, but you actually want it to be a char, so you'll see that Parse has inferred type string list -> 'a option. Therefore, F# expects Seq.toList to be applied to a string seq, not a string.

The simple fix is to change the line doing the printing to printf "%c " head.

Upvotes: 2

Related Questions