[Haskell-beginners] Handling failed output

Matt Williams matt.williams45.mw at gmail.com
Sat Apr 23 20:59:10 UTC 2016


Dear All,

I am trying to work out how to handle a function that might return 
different types of output. I assume I need to use either Maybe or 
Either, but I can't quite get it to work.

At the moment, I have some function:

checkNum3 :: Int -> Int -> (Int,Int)
checkNum3 a b = if check a b then (a,b)
                 else (a,-1)

checkLists :: [Int] -> Int -> [(Int,Int)]
checkLists a b = map (checkNum3 b) a

checkAll3 :: [Int] -> [Int] -> [(Int,Int)]
checkAll3 a b = concat (map (checkLists a) b)

However, I know that checkNum3 isn't a good function - it uses setting 
the second element of the tuple to -1 to signal failure, which is 
obviously a recipe for problems later on. However, I want to return 
either a pair of integers, or a single integer.

Any advice would be appreciated.

Thanks,
Matt


More information about the Beginners mailing list