Haskell Cheatsheet

1399




Sample program

main = do  
    name <- getLine  
    putStrLn ("Hello " ++ name ++ ", Happy learning!")
  • main : entry point of the program

  • getLine : reads input data from Console

  • putStrLn : prints data to the console.

  • -- : Single line comment

  • {- Multi

    Line

    comment -}

Data types

Data-typeDescription
NumbersHaskell is intelligent to identify numbers without specifying data type
CharactersHaskell is intelligent to identify characters and strings without specifying data type
TupleTo declare multiple values in a single data type. Tuples are represented in single paranthesis. For example (10,20,'apple')
BooleanTo represent boolean values, true or false
ListTo declare same type of values in a single data type. Lists are represented in square braces.For example [1,2,3] or `['a','b','c','d']

Reserved keywords

||||
|----|----|----|----|
| case | class | data| deriving|
| do | else | if |import|
| in | infix | infixl | infixr|
| instance | let | of | module|
| newtype | then | type | where

Operators

TypeOperators
Arithmetic Operators+ , - , * , / , % , **, &&, ||, not
Comparision Operators== , /= , > , >= , < , <=
Bitwise Operators& , ^ , | , ^ , ~ , << , >> , >>>
Function Operators, ., |
List Operators[...,...], ++, :, !!, .. , \, <-
Monad Operators:, ->, ::, =>, (), >>, >>=, >@>,(..)
Pattern Operators_, ~, !, @

Conditional Statements

If-Else

if conditional-expression
  then true statements 
  else false statements

Case

 case exp of
     Pattern1  -> action1
     Pattern2  -> action2
     _         -> else_action

Tuples

Tuples are used to group data of different types(similar type is also allowed)

(item1,item2,[itemn…])

Example

(1, "John", 35.2, true)

Lists

Lists are usually used to group data of similar types

[item1, item2,[itemn]]

Example

[1,2,3,4,5,6,7,8,9,10]
[1..10]

Both the above gives the same meaning as you can also use range to define lists

Some of the common list operations

List OperationsDescription
list1+list2to append two lists list1+list2
list1!!nto return element n from list1
head/last list1to retrieve the first/last element of the list1
sum list1to get the sum of all list elements present in list1
prod list1to get the product of all list elements present in list1
reverse list1to reverse the elements present in list1

Functions

How to define and call a Function

functionname :: data type [-> datatype] -> data type -- function definition 
--code

functionname(variable) -- calling a function

Examples

multiply :: Integer -> Integer -> Integer   --declaration of a function 
multiply x1 x2 =  x1 * x2                       --definition of a function

main = do 
   putStrLn "Multiplication value is:"  
   print(multiply 10 5)    --calling a function