简体   繁体   中英

How to use non-monadic functions in a bind operation

I feel like I should already know this, but how could I use fromMaybe in one line instead of breaking it into 2 with a let ?

main = do
    maybePort <- lookupEnv "PORT"
    let port = fromMaybe "4020" maybePort
    putStrLn $ "Listening on:" ++ port

you can use fmap or <$> like this:

import Control.Applicative ((<$>))

main = do
    port <- fromMaybe "4020" <$> lookupEnv "PORT"
    putStrLn $ "Listening on:" ++ port

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM