Haskell Best Practices: Haskeline Early Termination

I use the Haskeline package , and I want to get three lines per line from the command line before doing anything, and I came up with what seems like a neat solution to me. But I'm sure there might be a better way to do this. I am looking for best practices when using the Haskeline package. Take advantage of the following sample code:

import System.Console.Haskeline
import Control.Monad.Trans
import Control.Monad.Maybe
import Data.Maybe
import Control.Monad

main :: IO ()
main = runInputT defaultSettings (runMaybeT getStrings) >>= print

getStrings :: MaybeT (InputT IO) (String, String, String)
getStrings = do
   mone <- lift $ getInputLine "food> "
   notNothing mone
   mtwo <- lift $ getInputLine "drink> "
   notNothing mtwo
   mthree <- lift $ getInputLine "dessert> "
   notNothing mthree
   return (fromJust mone, fromJust mtwo, fromJust mthree)
      where
         notNothing a = guard (a /= Nothing)

As you can see, it performs the task of early termination, but it looks a bit so far. I am trying to convert notNothing and getInputLine to a single line, for example:

mone <- notNothing =<< lift $ getInputLine "food> " -- does not type check

What I think does not look so bad. I think this is pretty clear and concise (although it does not print a check, so I will have to write a version that does).

, , , : , ? ?

. - , "a/= Nothing", , :

myGuard s = guard (someConditionFunc s) >> s

( luqui):

mone <- myGuard =<< (lift $ getInputLine prompt)

. Nothing, TomMD.

+5
2

, fail _ = Nothing Maybe?

mthree <- lift $ getInputLine "dessert> "
notNothing mthree

Just mthree <- lift $ getInputLine "dessert> "
+7

?

inputLine :: String -> MaybeT (InputT IO) String
inputLine prompt = do
    m <- lift $ getInputLine prompt
    case m of
        Just x -> return x
        Nothing -> mzero

, , . , getInputLine , MaybeT .

+4

All Articles