Zulip Chat Archive
Stream: lean4
Topic: Terse code
Cole Shepherd (Dec 02 2022 at 01:55):
How would I combine these three lines, executing in the IO
monad, into one?
let input <- stdin.getLine
let guessAsStr := input.dropRightWhile Char.isWhitespace
let guess? := String.toNat? guessAsStr
Adam Topaz (Dec 02 2022 at 02:05):
Lean has do
notation that should work here
Cole Shepherd (Dec 02 2022 at 02:26):
These lines are actually already in a ‘do’ block. I’m trying to figure out how to eliminate the two intermediate variables
Adam Topaz (Dec 02 2022 at 02:29):
can you provide a #mwe ?
Scott Morrison (Dec 02 2022 at 02:42):
Does
let guess? := String.toNat? <| (<- stdin.getLine).dropRightWhile Char.isWhitespace
work?
Adam Topaz (Dec 02 2022 at 02:43):
Yeah I assume a mwe is something like this
def foo : IO Unit := do
let stdin ← getStdin
let guessAsStr := String.toNat? <| (← stdin.getLine).dropRightWhile Char.isWhitespace
Adam Topaz (Dec 02 2022 at 02:48):
I think this should have the same behavior (with a bit more ascii art)
let guessAsStr ← String.toNat? <$> (String.dropRightWhile · Char.isWhitespace) <$> stdin.getLine
Scott Morrison (Dec 02 2022 at 03:04):
I find the (← f)
syntax in Lean 4 so much easier to read/write/understand than "ascii art" (<$>
and friends).
Last updated: Dec 20 2023 at 11:08 UTC