What is functional programming?
Define what something is rather than how to compute it. You can write “functional code” in almost any language.
In general, programming languages in which the base method of computation is changing stored values are called
imperative languages. (Constructed from imperative instructions, that specify precisely how the computation should
proceed.) Functional programming can be viewed as a style of programming in which the basic method of computation is
the application of functions to arguments.
Some distinguishing features:
Recursion instead of iteration
Pattern matching on values
A function is defined by a series of equations and the value is compared with each left side until one “fits”.
Expression instead of statements
Functions as first-class citizens
Function is a mapping of arguments to a result
Functions are high-order, and thus can be used by other functions (example is the map function)
Function can be returned from functions
Using purely functional programming, programs will be easy to reason about because:
Data flow only through function arguments and return values
no hidden data-flow through mutable variables/state
Function call and return as only control flow primitive
no loops, break, continue, goto
(almost) unique types
no inheritance hell
high-level declarative data-structures
The Glasgow Haskell Compiler
In Haskell the Main module — the one containing your main function, is the function that will be invoked when you
execute your compiled program. The function must always be named main . (As long as you do not try to import it from
another module, you could name the file differently. Though, there is no reason to do so and you probably shouldn’t!)
Basic syntax
,Comments
Syntax
!-- comment ordinary comment
{- comment -} nested comment; multiline
Conditionals
< , > , !<= , !>= , !== are the same as in many languages. !/= means not equal.
!|| and !&& are OR and AND.
not is not.
You can use ‘ if condition then expression else expression’, although they are not used a lot. Here, you always need
to use both branches.
Naming requirements
Names of functions and its arguments have to begin with a lower-case letter, which can be followed by lower- and upper-
case letters, digits, underscores and forward single quotes. Some words have special meaning in the language and cannot
be used.
Layout rule
Within a script, each definition at the same level must begin in precisely the same column.
Grouping can be made explicit by using curly parentheses and separating each definition by a semi-colon, but this is
usually not preferable.
You can avoid nested if then else expression with guards (which is considered to be good style).
Be aware: For Haskell, you should not use tabs, but instead (2) spaces.
Types
In Haskell, every expression has a type. A type is a collection of related values.
Haskell is statically typed, which means it forbids executing code with type errors. Every term has a type. Once type is
defined, you’re not able to change it.
, Type Inference is the calculation of the type, when no type is given for an expression (it will “guess” one for you). There is a
clear rule for type inference:
Type declaration
We use the notion v !:: T to mean that v is a value in the type T and say v has type T . With :t / :type in
GHCi you can get the type declaration. For example :t True returns:
True !:: Bool
Basic types
Type Description
Bool logical values, either True or False
Char single characters in Unicode, denoted with single quotes ‘
String sequence of characters, denoted with double quotes “
Int integers with fixed range and thus memory; till
Integer integer with unlimited range
single-precision floating-point numbers, fixed amount of memory; number of digits after the decimal point
Float
depends upon the size of the number (32-bits)
Double double-precision floating-point numbers, take up more space (64-bits)
Lists
Lists are sequences of elements of the same type, and are denoted by squared brackets, e.g.
primeNumbers = [3,5,7,11]