<div dir="ltr">Hi,<br><br>Using LambdaCase extension, you can write something quite elegant:<br><br>{-# LANGUAGE LambdaCase #-}<br><br>f :: Int -> IO String<br>f x = getDBRecord x >>= \case<br> dbOutput <br> | null dbOutput -> return "no db record"<br> | otherwise -> return "we got some db records"<br><br>Or better:<br><br>f :: Int -> IO String<br>f x = getDBRecord x >>= \case<br> [] -> return "no db record"<br> _ -> return "we got some db records"<br><br>But you can also use another function for the pure part:<br><br>recordMsg :: [a] -> String<br>recordMsg [] = "no db record"<br>recordMsg _ = "we got some db records"<br><br>f :: Int -> IO String<br>f = fmap recordMsg . getDBRecord<br><br>Regards,<br>Sylvain<br><br><div class="gmail_extra"><br><div class="gmail_quote">2015-01-15 12:51 GMT+01:00 Miro Karpis <span dir="ltr"><<a href="mailto:miroslav.karpis@gmail.com" target="_blank">miroslav.karpis@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div><div>Hi,<br><br></div>please is there a way to have guards with 'where' that communicates with IO? Or is there some other more elegant way? I can do this with classic if/else,...but I just find it nicer with guards.<br><br><br></div>I have something like this (just an example):<br><br><br>f :: Int -> IO String<br>f x<br> | null dbOutput = return "no db record"<br> | otherwise = return "we got some db records"<br> where dbOutput = getDBRecord x<br><br><br>getDBRecord :: Int -> IO [Int]<br>getDBRecord recordId = do<br> putStrLn $ "checking dbRecord" ++ show recordId<br> --getting data from DB<br> return [1,2]<br><br><br></div><div>problem is that db dbOutput is IO and the guard check does not like it:<br> <br>Couldn't match expected type ‘[a0]’ with actual type ‘IO [Int]’<br> In the first argument of ‘null’, namely ‘dbOutput’<br> In the expression: null dbOutput<br><br><br></div><div><br></div>Cheers,<br></div>Miro<br></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div></div>