In my previous
post, I explained about how to define a variable. We can also specify type to a
variable.
Syntax
variable ::
Type
Sample.hs
name :: String pin :: Integer name = "Krishna" pin = 123456
Prelude> :load Sample.hs [1 of 1] Compiling Main ( Sample.hs, interpreted ) Ok, modules loaded: Main. *Main> *Main> name "Krishna" *Main> *Main> pin 123456 *Main> *Main> :t name name :: String *Main> *Main> :t pin pin :: Integer
You can combine the binding and type
declaration in one line like below.
Sample.hs
name = "Krishna" :: String pin = 123456 :: Integer
If all variables has same type, then you
can declare them in same line like below.
Sample.hs
a, b, c, d :: Integer e, f :: String a = 10 b = 11 c = 12 d = 13 e = "ptr" f = "nayan"
*Main> :load Sample.hs [1 of 1] Compiling Main ( Sample.hs, interpreted ) Ok, modules loaded: Main. *Main> *Main> (a, b, c, d, e, f) (10,11,12,13,"ptr","nayan")
By enabling scoped type variables, you
can specify the type signature and assign a values to different type variables in same
line. ScopedTypeVariables is a GHCi extension.
Sample.hs
{-# LANGUAGE ScopedTypeVariables #-} (empId :: Integer, name :: String, salary :: Double) = (10, "Sudhir", 5.7)
*Main> :load Sample.hs [1 of 1] Compiling Main ( Sample.hs, interpreted ) Ok, modules loaded: Main. *Main> *Main> empId 10 *Main> *Main> name "Sudhir" *Main> *Main> salary 5.7
No comments:
Post a Comment