Haskell

In purely functional programming you don't tell the computer what to do but rather you tell it what stuff is. (e.g think to factorial of a number)

Referential transparency: regardless of any reference (location), the evaluation of an expression (variable, function ..) still the same (over time).
(consequence: no side effects).

Lazy evaluation: 
e.g. suppose there is no laziness, and you'd like to apply a function on a list.
==> you would probably pass through the list once and make a copy and then return it. Then it would pass through the list another two times and return the result.

Statically typed + type inference
(Pros of type inference : e.g. if a function takes two operand and apply an operator on them, this function will work on any type).

Functions in Haskell starts with a lowercase, and arguments are space-separated (no parenthesis).

If-statement in Haskell is "an expression", and the else-part is mandatory.

Enumeration: a way of making lists that are arithmetic sequences of elements that can be enumerated. e.g. Numbers (One, two, three, four ...), Alphabet (from A to Z). 
Names can't be enumerated. What comes after "Lahcen"? I don't know.  

List comprehensions:
transformation xs = [mapper x | x <- xs, predicate1, predicate2, predicate3 ...]

example:
removeOdds xxs = [  [ x | x <- xs, even x ]  |  xs <- xxs ]

input:    [[1,3,5,2,3,1,2,4,5],[1,2,3,4,5,6,7,8,9],[1,2,4,2,1,6,3,1,3,2,3,6]]

output:  [[2,2,4],[2,4,6,8],[2,4,2,6,2,6]]


Contrast:
List: finite or infinite + homogeneous (one type)
Tuple: when you know in advance how many values you want to combine + can be heterogeneous.

Type: any object has a type, examples: 
Prelude> :t head
head :: [a] -> a  (function takes a list and return an element)
 

Prelude> :t "hello"
"hello" :: [Char]  (a list of char, meaning a string actually)


Typeclass: a sort of interface that defines some behaviour. If a type is a part of a typeclass, that means that it supports and implements the behaviour the typeclass describes.example:

Prelude> :t 5
5 :: Num t => t (the part before "
=>" is the typeclass, meaning that: 5 has a type 'Int' -not displayed here-, and a typecalss 'Number').

Type annotations: are a way of explicitly saying what the type of an expression should be. We do that by adding :: at the end of the expression and then specifying a type. Observe: 
Prelude> read "5" :: Float
5.0



Pattern matching: piecewise functions.
Pattern matching is a syntactic sugar of case expressions
let binding is an expression used for local scoping
where binding is a syntactic sugar of let binding



No comments: