I would not say that the problem is with the guard check. The problem is with 'null'. It's type is

Prelude> :t null
null :: [a] -> Bool

So, it expects a list of something, rather than an IO of something, whence the complaint.

Dimitri


On 15/01/15 09:51, Miro Karpis wrote:
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


_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners