This is a simple question with a complex answer I presume.
A very common programming problem is a function that returns something, or fails precondition checks. In Java I would use some assert function that throws IllegalArgumentException
at the beginning of the method like so:
{
//method body
Assert.isNotNull(foo);
Assert.hasText(bar)
return magic(foo, bar);
}
What I like about this is that it is a oneliner for each precondition. What I don't like about this is that an exception is thrown (because exception ~ goto).
In Scala I've worked with Either, which was a bit clunky, but better than throwing exceptions.
Someone suggested to me:
putStone stone originalBoard = case attemptedSuicide of
True -> Nothing
False -> Just boardAfterMove
where {
attemptedSuicide = undefined
boardAfterMove = undefined
}
What I don't like is that the emphasis is put on the True and the False, which mean nothing by themselves; the attemptedSuicide
precondition is hiding in between syntax, so not clearly related to the Nothing AND the actual implementation of putStone
(boardAfterMove) is not clearly the core logic. To boot it doesn't compile, but I'm sure that that doesn't undermine the validity of my question.
What is are the ways precondition checking can be done cleanly in Haskell?