Search code examples
f#filesystemsfsharpchart

Fsharp - types compilation error


I am new to F#, and I'd love to get some help :)

I have ? compilation error on this code, and I can't figure it out:

printfn "Please enter the path for the Jack file/s directory"
let dir = System.Console.ReadLine()
let jackFiles : List<String> = (new List<String>())
dir
|> Directory.GetFiles
|> Seq.iteri(fun file -> if ((Path.GetExtension(file)).Equals(".jack")) then JackFiles.Add(file))

The compiler shouts this error:

This expression was expected to have type string->unit but here has type unit

about the if ((Path.GetExtension(file)).Equals(".jack")) then JackFiles.Add(file)) part...

Why is it wrong and how do I fix it?


Solution

  • The function argument to Seq.iteri requires two arguments and should have type (int -> 'T -> unit). Its file parameter is inferred to have type int and therefore your if statement should have type string -> unit but actually has type unit, hence the error.

    It looks like you don't require the int argument so you can use Seq.iter instead.

    However it looks like what you're trying to do could be done using something similar to:

    let jackFiles = Directory.GetFiles(dir, "*.jack")