Haskell:检查是否为整数,或检查变量的类型 [英] Haskell: Check if integer, or check type of variable
本文介绍了Haskell:检查是否为整数,或检查变量的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设您有一个变量 n.
So let's say you have a variable n.
你想检查它是否是一个整数,或者更好的是检查它是什么类型.
You want to check if its an integer, or even better yet check what type it is.
我知道 haskell 中有一个函数 isDigit 可以检查它是否是一个字符.
I know there is a function in haskell, isDigit that checks if it is a char.
但是有没有一个函数可以检查 n 是否是整数,或者更好的是,给出 n 的类型?
However is there a function that checks if n is in integer, or even better, gives the type of n?
推荐答案
如果您使用的是交互式 Haskell 提示(如 GHCi),您可以输入 :t
,这会给您表达式的类型.
If you are using an interactive Haskell prompt (like GHCi) you can type :t <expression>
and that will give you the type of an expression.
例如
Prelude> :t 9
给予
9 :: (Num t) => t
或例如
Prelude> :t (+)
给予
(+) :: (Num a) => a -> a -> a
这篇关于Haskell:检查是否为整数,或检查变量的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文