Haskell: Check if integer, or check type of variable

前端 未结 2 1003
死守一世寂寞
死守一世寂寞 2020-12-05 15:28

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

相关标签:
2条回答
  • 2020-12-05 15:59

    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
    
    0 讨论(0)
  • 2020-12-05 16:04
    
    import Data.Typeable
    isInteger :: (Typeable a) => a -> Bool
    isInteger n = typeOf n == typeOf 1
    

    But you should think about your code, this is not very much like Haskell should be, and it probably is not what you want.

    0 讨论(0)
提交回复
热议问题