Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use non-monadic functions in a bind operation

Tags:

haskell

monads

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
like image 493
Sean Clark Hess Avatar asked Feb 12 '23 17:02

Sean Clark Hess


1 Answers

you can use fmap or <$> like this:

import Control.Applicative ((<$>))

main = do
    port <- fromMaybe "4020" <$> lookupEnv "PORT"
    putStrLn $ "Listening on:" ++ port
like image 98
Random Dev Avatar answered Feb 20 '23 09:02

Random Dev