Types Flashcards
How to get type of an expression
using :t operator
How to read :: operator
has type of
Evaluate :t “Hello!”
“Hello!” :: [Char]
what is the meaning of -> in type definitions
it is read as ‘maps’ ; so if Int -> Int is seen in the type declaration, it means this function maps Int parameter to Int result
In case of type definition of a function which are parameters and which is for output
Output type is specified as last item in the list separated by ->
Define Int type
Stands for 32 bit integers, both +ve and -ve
Define Integer type
unbounded integer size
Define Float type
Single precision floating point
Define Double type
Double precision floating point
Define Bool type
type boolean; has only two values True and False
Define Char type
used for single characters. string is a list of chars.
What are polymorphic functions
functions having type variables are called polymorphic
Difference between infix and postfix functions; provide examples
Oeprators are infix functionos, where the operator <param2} is the syntax for intfix functions. These are alled as operatrors as well
Describe ‘Eq’ typeclass
used for types that support equality testing, Members of this type class implement == and /==. If there is a Eq in the type description of a function, it uses in its definition somewhere either == or /== operators
What Haskell functions will not implement ‘Eq’ typeclass
IO functions
Describe ‘Ord’ typeclass
used for types that support ordering. For example operators like , =, LT, GT, or EQ
what is the prerequisite of ‘Ord’ typeclass members?
They must be part of ‘Eq’ typeclass as well.
Describe Show typeclass
Members of “Show” typeclass can be presented as strings using the function show. for example show 3 will return “3”
What are not members of ‘Show’ type class?
all functions
Describe Read typeclass
Opposite to Show. Gets a string and returns a type which is a member of “Read”
Evaluate read “5” - 3
2
Evaluate read “[1,2,3,4]” ++ [5]
[1,2,3,4,5]
Evaluate read “True” || False
True
Evaluate read “4”
GHCI will crash, since it does not know what to do with the return value !!!
How to specify ‘type annotations’ on read?
Use :: operator after the read. for example
read “4” :: Int
Show an example of ‘type annotator’ in an expression
(read “5” :: Float) * 4 which will return 20.0
why the expression read “4” crashes?
Since Haskell uses type inference, it has no context to infer the output type from this expression
Define ‘Enum’ typeclass
Its members are types which can be ordered sequentially aka enumerated
Describe advantages of Enum class
Its members types can be used in ranges;
They can also be used with succ and pred functions
List all types in Enum class
(), Bool, Char, Float, Double, Int, Integer, and Ordering
Describe Bounded typeclass
Member types can have an upper and lower limit
Evaluate minBound :: Float
Need to check with GHCI and update the answer here
Evaluate maxBound :: Float
Need to check with GHCI and update the answer here
Evaluate minBound :: Char
What is the value returned by GHCI? Is this ASCII limit or something else..
Evaluate miinBound :: (Int,Bool)
(-2147483648, False)
Describe Num typeclass
Its members behave as numbers; So Int, Integer, Float, and Double are its members
Describe class constraint
In a type description anything before => is known as class constraint. For example
:t (==)
(==) :: (Eq a) => a -> a -> Bool
Here the class constraint is saying the parameter should belong to type class Eq