6
\$\begingroup\$

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
Tunaki
9,3011 gold badge31 silver badges46 bronze badges
asked Apr 13, 2016 at 15:33
\$\endgroup\$
1
  • \$\begingroup\$ Hi, welcome to Code Review! I hope you receive great answers! \$\endgroup\$ Commented Apr 13, 2016 at 16:04

2 Answers 2

5
\$\begingroup\$

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
answered Apr 13, 2016 at 16:05
\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Apr 13, 2016 at 20:35
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Commented Apr 13, 2016 at 21:35
1
\$\begingroup\$

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
 match stdin.ReadLine () with
 | value when predicate value -> value
 | _ -> repeatingPrompt predicate message
answered Jul 8, 2016 at 23:11
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.