2
votes

I`m trying to make some code to keep asking the user until a valid file path or "exit" is input.

What I have now:

let (|ValidPath|_|) str =
    if File.Exists str then Some ValidPath
    else None

type Return =
    |Path of string
    |Exit

let rec ask =
    printfn "%s" "Please enter a valid path or \"exit\""
    let input = Console.ReadLine()
    match input with
    | "exit" -> Exit
    | ValidPath -> Path input
    | _ -> ask

The ask function has a The value 'aks' will be evaluated as part of its own definition error.

What can I do?

1

1 Answers

4
votes

The problem is that ask isn't a function, it's a recursive value. You need to take a parameter in order to make it a function:

let rec ask () =
    printfn "%s" "Please enter a valid path or \"exit\""
    let input = Console.ReadLine()
    match input with
    | "exit" -> Exit
    | ValidPath -> Path input
    | _ -> ask ()