Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)
    let value = Console.ReadLine()
    if predicate value then
        value
    else
        repeatingPrompt predicate message
share|improve this question
    
Hi, welcome to Code Review! I hope you receive great answers! – Tunaki Apr 13 at 16:04
    
@Tunaki thanks! – Grubl3r Apr 13 at 20:29
up vote 5 down vote accepted

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)
        Console.ReadLine()

    Seq.initInfinite prompt |> Seq.find predicate
share|improve this answer
    
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 at 20:35
1  
@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 at 21:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.