Tengo el siguiente código pero creo que es demasiado feo e imperativo. ¿Alguien lo reformularía para ser más funcional? (Me metí con MaybeT pero no pude hacerlo funcionar) Las respuestas aplicables también son bienvenidas.IO y quizás Interacción de mónada
getString :: IO String
pred :: String -> Bool
f :: String -> String
result :: IO (Maybe String)
result = do
s <- getString
if pred s
then return $ Just $ f s
else return Nothing
EDIT: Una pregunta de seguimiento: ¿y si ambos de Pred y F también proporcione resultados dentro de IO (debo dividir esto en una pregunta separada?)
getString :: IO String
pred :: String -> IO Bool
f :: String -> IO String
result :: IO (Maybe String)
result = do
s <- getString
b <- pred s
if b
then Just <$> f s
else return Nothing
+1 No se puede negar que obtener la lógica de 'IO' es más importante que tener una solución elegante de una sola línea. – leftaroundabout