In an ideal world, you should avoid functions like head and have no errors at all at runtime. Runtime errors are mostly inconvenient , especially in Haskell.
In this case, you want to catch the runtime error. If you want to turn it into Maybe , you can use the spoon package.
Haskell's exceptions are subtle due to laziness. In particular, if you do not evaluate the part of the structure that has an exception, it does not work. This is done in a spoon with two different functions:
spoon , which deeply evaluates the structure, but requires an instance of a special typeteaspoon that evaluates your structural part of the path of a weak normal head shape
In your case, I think teaspoon should be fine. Try checking the result of the analysis:
teaspoon (moveSAN move board)
which should give you a Maybe value.
If this does not work, it means that you need to evaluate more structure in order to get into the exception. Board doesn't seem to implement the class needed for a deep evaluation with spoon , so your best bet is a bit hacked: use spoon for the show result:
spoon (show $ moveSAN move board)
This will give you Maybe String . If he is Just , the move will be correct; if it is Nothing , an error has occurred.
It is worth noting that the spoon package is actually not very much: each function has only a couple of lines . At some point, it's worth figuring out how to handle exceptions in Haskell yourself, but for now, spoon just a little more convenient and should work correctly for you.
source share