当我在GHCi中测试文字类型时,我发现
Prelude> :t 1
1 :: Num p => p
Prelude> :t 'c'
'c' :: Char
Prelude> :t "string"
"string" :: [Char]
Prelude> :t 1.0
1.0 :: Fractional p => p
Run Code Online (Sandbox Code Playgroud)
问题是Haskell如何确定这种文字的类型?我在哪里可以找到有关它的信息?
此外,是否有任何方法可以改变GHC解释文字类型的方式?
例如:
-- do something
:t 1
1 :: Int -- interprets 1 as Int rather then Num p => p
:t 1.0
1.0 :: Double -- interprets 1.0 as Double rather then Fractional p => p
Run Code Online (Sandbox Code Playgroud)
提前致谢.
在 F# 交互中尝试以下代码时
> let a = None
- let b = (a, Some 1);;
> b;;
val it : 'a option * int option = (null, Some 1)
Run Code Online (Sandbox Code Playgroud)
它表明 b 的类型为'a option * int option,b 的类型是正确的。但是,元组的第一个元素的值为 null,而不是 None,为什么?
当尝试验证元组的第一个元素是否确实为 null 时
printfn "%s" (match b with (null, _) -> "null" | _ -> "not null");;
Run Code Online (Sandbox Code Playgroud)
它给出以下错误
错误 FS0043:类型“a option”没有“null”作为正确值
当尝试获取元组中的第一个值时,
let c = fst b;;
Run Code Online (Sandbox Code Playgroud)
它给
错误 FS0030:值限制。值 'c' 已被推断为具有泛型类型 val c : '_a option 将 'c' …