我正在尝试找出如何编写Haskell版本的assert(0)
的最佳实践。我知道类型安全要求必须从scanList
返回一个整数,但我想知道是否有比我编写的更好的方法。有没有办法避免只是固定在那里的任意数字923
?
module Main (main) where
import Control.Exception (assert)
l = [
Left "1",
Left "1",
Right 1,
Right 1,
Left "9"]
scanList :: [ Either String Int ] -> Int
scanList [ ] = 0
scanList (x:xs) = case x of
Right i -> i + scanList xs
Left s ->
if read s < 8
then read s + scanList xs
else assert False $ 923
main = do
print $ scanList l
assert
调用没有开销。 - jberryman