Hind Forsum
Hind Forsum

Reputation: 10497

How to specify a value's type in Haskell's Prelude?

I know Haskell is able to deduce a value's type, but I wish to specify it, I tried:

Prelude> a=2 :: Float

<interactive>:8:2: parse error on input ‘=’
Prelude> Float a=2 :: Float

<interactive>:9:8: parse error on input ‘=’
Prelude> let a::Float = 2

<interactive>:10:8:
    Illegal type signature: ‘Float’
      Perhaps you intended to use ScopedTypeVariables
    In a pattern type-signature
Prelude> let a::Int = 2

<interactive>:11:8:
    Illegal type signature: ‘Int’
      Perhaps you intended to use ScopedTypeVariables
    In a pattern type-signature

None succeeded. How to achieve it?

BTW, seems in Haskell everything(almost) is a function and is immutable. So should I call "a":

1. An object? (FP is not OOP)
2. Or, a function?
3. Or, a variable?
4. Or, a value?

Not sure what term does Haskell prefer? Thanks.

Upvotes: 0

Views: 81

Answers (2)

awesoon
awesoon

Reputation: 33651

ghci is an interactive console, so, you cannot just write a = 2 :: Float, you have to use let form:

Prelude> let a = 2 :: Float
Prelude> a
2.0
Prelude> :t a
a :: Float

BTW, seems in Haskell everything(almost) is a function and is immutable. So should I call "a":

a is a value of type Float

Upvotes: 3

Fraser
Fraser

Reputation: 1521

Prelude> let a = 2 :: Int
Prelude> a
2

a is a value, it's not a function (but functions are values).

Upvotes: 3

Related Questions