[Haskell-beginners] help with IO guards

Julian Birch julian.birch at gmail.com
Thu Jan 15 12:51:53 UTC 2015

Here's how I would do it:

Write two functions. One (f) takes the int and gets the IO record. One
(g) takes a (non-IO) record and returns the string. Now you can use a bind
(=<<) to combine the two.

Once you've got that working, you can move the bits into the where clause
of your original function.

Hope this helps.


On Thursday, January 15, 2015, Miro Karpis <miroslav.karpis at gmail.com>

> Hi,
> 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.
> I have something like this (just an example):
> f :: Int -> IO String
> f x
>     | null dbOutput = return "no db record"
>     | otherwise = return "we got some db records"
>     where dbOutput = getDBRecord x
> getDBRecord :: Int -> IO [Int]
> getDBRecord recordId = do
>     putStrLn $ "checking dbRecord" ++ show recordId
>     --getting data from DB
>     return [1,2]
> problem is that db dbOutput is IO and the guard check does not like it:
> Couldn't match expected type ‘[a0]’ with actual type ‘IO [Int]’
>     In the first argument of ‘null’, namely ‘dbOutput’
>     In the expression: null dbOutput
> Cheers,
> Miro

Sent from an iPhone, please excuse brevity and typos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150115/b8d237eb/attachment.html>

More information about the Beginners mailing list