您好如何执行GHC
函数类型,例如 Data.Text.read
或 =~
来自 Text.Regex.Posix
的运算符编写方法时?
例子:a=["1.22","3.33","5.55"]
没有免费点:b= map (\x-> read x ::Double) a
如何强制 read
的类型用无点符号?b=map read::Double a
或者
b= map (read . f1 .f2 .f3... . fn )::Double a
(在编写方法时)其中 f1 , f2 ...fn
是方法
或者更好的是如何指定 read
属于方法链时的类型,但是 不在链的末端! :b=map (f2 . read . f1 ) a
最佳答案
现代 Haskell 中最好的方法是使用 type application .
Prelude> :set -XTypeApplications
Prelude> map (read @Double) ["1.22","3.33","5.55"]
[1.22,3.33,5.55]
Prelude> map (read @Int) ["1.22","3.33","5.55"]
[*** Exception: Prelude.read: no parse
这是因为
read
有签名read :: ∀ a . Read a => String -> a
因此
read @Double
专业 a ~ Double
因此read @Double :: String -> Double
关于haskell - 使用无点表示法时如何强制类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52275817/