
I've defined a function similar to check x assoc = let found = lookup x assoc in when (isJust found) $ putStrLn $ "found " ++ fromJust found which I've used several times from within the IO monad. Is there a more compact way of doing this?

I'd rather use pattern matching in this case:
check x assoc = case lookup x of
Just found -> putStrLn ("found " ++ found)
_ ->
Not really shorter but I think it's cleaner this way.
On Thu, Aug 1, 2013 at 5:00 PM, vold
I've defined a function similar to
check x assoc = let found = lookup x assoc in when (isJust found) $ putStrLn $ "found " ++ fromJust found
which I've used several times from within the IO monad. Is there a more compact way of doing this?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Of course there is "return ()" missing.
check x assoc = case lookup x of
Just found -> putStrLn ("found " ++ found)
_ -> return ()
On Thu, Aug 1, 2013 at 5:34 PM, Krzysztof Skrzętnicki
I'd rather use pattern matching in this case:
check x assoc = case lookup x of Just found -> putStrLn ("found " ++ found) _ ->
Not really shorter but I think it's cleaner this way.
On Thu, Aug 1, 2013 at 5:00 PM, vold
wrote: I've defined a function similar to
check x assoc = let found = lookup x assoc in when (isJust found) $ putStrLn $ "found " ++ fromJust found
which I've used several times from within the IO monad. Is there a more compact way of doing this?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Thu, Aug 1, 2013 at 6:00 PM, vold
I've defined a function similar to
check x assoc = let found = lookup x assoc in when (isJust found) $ putStrLn $ "found " ++ fromJust found
which I've used several times from within the IO monad. Is there a more compact way of doing this? Firstly, note that it is more idiomatic to move the IO code to other functions, which would allow check to be pure. Secondly, one can use the fact that Maybe is a Functor to rewrite check as follows: check x db = maybeToIO $ (putStrLn . ("found"++)) <$> x `lookup` db where maybeToIO = fromMaybe (return ()) The refactoring of maybeToIO isn't strictly necessary, but I found it clearer. HTH, Gesh

On 08/01/2013 11:00 AM, vold wrote:
I've defined a function similar to
check x assoc = let found = lookup x assoc in when (isJust found) $ putStrLn $ "found " ++ fromJust found
which I've used several times from within the IO monad. Is there a more compact way of doing this?
maybe (return ()) (putStrLn . ("found " ++)) found is the best I could do.

With Data.Foldable, it's quite nice:
check x assoc = lookup x assoc `for_` \f -> putStrLn ("found " ++ f)
On Fri, Aug 2, 2013 at 1:45 AM, Michael Orlitzky
On 08/01/2013 11:00 AM, vold wrote:
I've defined a function similar to
check x assoc = let found = lookup x assoc in when (isJust found) $ putStrLn $ "found " ++ fromJust found
which I've used several times from within the IO monad. Is there a more compact way of doing this?
maybe (return ()) (putStrLn . ("found " ++)) found
is the best I could do.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (5)
-
Gesh hseG
-
Krzysztof Skrzętnicki
-
Michael Orlitzky
-
Mike Ledger
-
vold