如果我定义
import Control.Exception (assert)
import Data.Char (ord)
f :: String -> String
f s = assert (all (`elem` letters) s) $ (letters!!) <$> (ix <$> s)
where
ix ch = (ord ch - ord 'A')
letters = ['A'..'Z']
那么如果我执行
f "AB.CD"
在 IHaskell 中,我得到
:10:7-12: Assertion failed
正如我所期望的。但在所有其他设置中,该断言似乎被忽略。例如在 GHCi (7.10.2) 中我得到
ghci>f "AB.CD"
"AB*** Exception: Prelude.!!: negative index
如果我将表达式放入程序中
main :: IO ()
main = do
print $ f "AB.CD"
I get
prgm: Prelude.!!: negative index
"AB
为什么除了 IHaskell 之外,我的断言在其他地方都被忽略了?
在 GHCi 中,:set
gives:
options currently set: none.
base language is: Haskell2010
with the following modifiers:
-XNoDatatypeContexts
-XNondecreasingIndentation
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
-fimplicit-import-qualified
warning settings:
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)