Haskell:检查整数,或检查变量的类型 [英] Haskell: Check if integer, or check type of variable

查看:106
本文介绍了Haskell:检查整数,或检查变量的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个变量n。

你想检查它是否是一个整数,或者更好的检查它是什么类型。



我知道haskell中有一个函数,isDigit检查它是否是char。



然而,有一个函数检查n是否是整数,或者甚至更好,给出了n的类型?

解决方案

如果您正在使用一个交互式的Haskell提示符(如GHCi),你可以输入:t< expression> ,这会给你一个表达式的类型。



eg

  Prelude> :t 9 

给出

  9 ::(Num t)=> t 

或者例如

 前奏> :t(+)

给出

 (+)::(Num a)=> a  - > a  - > a 


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.

I know there is a function in haskell, isDigit that checks if it is a char.

However is there a function that checks if n is in integer, or even better, gives the type of n?

解决方案

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.

e.g.

Prelude> :t 9

gives

9 :: (Num t) => t

or e.g.

Prelude> :t (+)

gives

(+) :: (Num a) => a -> a -> a

这篇关于Haskell:检查整数,或检查变量的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆