# Console input prompt loop in F#

I'm learning F#, starting with baby steps. Is there a way to write this code in a more succint way, while keeping the same semantics? Maybe by using a Seq or something like that?

let rec repeatingPrompt predicate (message:string) =
Console.WriteLine(message)
if predicate value then
value
else
repeatingPrompt predicate message

• Hi, welcome to Code Review! I hope you receive great answers! – Tunaki Apr 13 '16 at 16:04

You can use Seq.initInfinite and Seq.find to do this, though I'm not sure it's actually better:

let repeatingPrompt predicate (message:string) =
let prompt _ =
Console.WriteLine(message)

Seq.initInfinite prompt |> Seq.find predicate

• thank you. Why do you think it is not so useful to do this? Would you have done something along the lines of what I wrote? – Grubl3r Apr 13 '16 at 20:35
• @Grubl3r Possibly. Both versions are about the same length, but the Seq version is harder to understand, since you need to know what initInfinite and find do. – svick Apr 13 '16 at 21:35

I like svick's answer but wanted to show an alternate way without using Seq module.
It's not very different of yours (not really an improvement), but functional programming tend to favor pattern matching over if tests so maybe it's more "idiomatic".

I also replaced the use of Console with the already available (without opening System) stdin and stdout but that's anecdotic

let rec repeatingPrompt predicate message =
stdout.WriteLine (message: string) // you can put the type here too