<aside> ⚙ Commands on GHCI ❯ ghci: open interative terminal ❯ :r: recompile file ❯ :l File.hs: run the file, add module Module where (it able to run file without extension) ❯ :t Value: checks the data type
</aside>
<aside> 📌 **Basic Concepts
Types
→** Char: 'H'
→ [Char] or String: "Hello World!"
****→ Int: 1
→ Bool: True/False
→ creating new data type: data Day = Monday | Sunday
**new type is Day and it has value constructors (Monday or Sunday)
→ Record syntax: data Point = Point {x, y :: Double}
→ creating new type with only one field: newtype Data = Data Int
**lazy evaluation
Data Structure
→ List: [1, 2, 3, 4]
**same element type and dynamic length
→ Tuples: (1, "Hello", 2, 'j')
**several element type and fixed-length
Operators
→ maths operations: + - * /
→ conditions operations: True False not && ||
→ equal, not equal: == /=
**→ sucessor: succ 8
→ min max: min max [1, 2]
→ div (integral division): div 2 3
→ mod (modular division): mod 2 3
→ odd | even (check is number is odd | even): odd|even 2
**Conditional Statement
→ if x > 100 then x else x*2
Functions
→ nameFunction p1 p2 p3 = expression
→ call the function: nameFunction p1 p2 p3
Pattern Matching;
→ Inspect parts of a value constructor
→ h [] = 0
h (_:[]) = 1
** _ ignore value
h (_:x:[]) = 2 + x
**x accepts any value
</aside>
<aside>
💡 Typeclasses Highlights
→ A typeclass is a sort of interface that defines some behavior
→ If a type is a part of a typeclass, that means that it supports and implements the behavior the typeclass describes
→ Eq: used for types that support equality testing, implement == and /=
→ Show: all types covered so far except for functions are a part of this class, it takes a value whose type is a member of Show and presents it to us as a string
→ Read: sort of the opposite typeclass of Show, it takes a string and returns a type which is a member of Read
→ Num: numeric typeclass, its members have the property of being able to act as numbers. It has the functions: *abs*
(transform negative values on positives one); *signum*
(indicate if the number is positive, negative or zero); *fromInteger*
(convert the interger number to other type)
→ Fractional: it provides a way to use the division for your type. It has the function *fromRational*
(convert a fraction in another type)
→ Real: it provides the function *toRational*
(convert the type to a Fractional)
→ Integral: includes only integral (whole) numbers. In this typeclass are Int and Integer. It has the functions *quotRem*
(return the quotient and the rest); *toInteger*
(convert the type to an Integer)
→ Enum: members are sequentially ordered types — they can be enumerated. It has the functions: *succ*
(get the successors values) and *pred*
(get ht predecessors values).
→ Ord: for types that have an ordering with >, <, >= and ≤, it has the *compare*
that returns GT, LT or EQ; also *max*
and *min
*
→ Bounded: members have an upper and a lower bound. it has the *minBound*
and *maxBound
*
Creating a new class: class SimNao a where simnao :: a -> Bool
</aside>
<aside> 🌟 List Highlights
→ to join lists: *[1, 2] ++ [3, 4]
(++) [1, 2] [3, 4]
or "Hello" ++ " World!"
→* to push a new element on the first position: 3 : 4 : 4 : [10, 9, 4]
→ to get list length: length [1, 2, 3]
→ to get the first element: *head "ABCD"
or head [1, 2]
→ to get last element: *last "ABCD"
or last [1, 2]
→ to get the list without first element: tail "ABCD"
or tail [1, 2]
** head, last and tail are not refined to empty lists
→ to get all elements except the last one: init "ABCD"
or init [1, 2]
→ to reverse a list: *reverse "HASKELL"
or reverse [1, 2, 3]
→ to extract elements from the beginning: take n [2, 4, 5]
→* to extract elements dropping n of the begin*: drop n [1, 2]
→* to get element on index: [1, 2, 3] !! 2
→ to check if a element exist inside a list: *4
elem [3,4,5,6]*
→ to check if a list is empty: null [1, 2]
→ to sum list of numbers: sum [1, 2]
→ to multiply the list of numbers: product [1, 2]
→ to get the max or min of a list of numbers: maximum | minimum [1, 2]
→ get the element and the rest*: x:xs
*
Ranges
→ to produce a value range: *[1..20]*
→ to cycle into an infinite list: *take 12 (cycle "LOL ")*
→ to produce an infinite list of just one element: *take 10 (repeat 5)*
→ to replicate a number into a list: *replicate 3 10*
**List Comprehensions
→ to build lists using expressions that will be distributed in each element
→ [EXPRESSION(var) | var←LIST, FILTER_1, FILTER_2...FILTER_n]
→ doubleList xs = [2*x | x←xs]
→ list = [2*x+1 | x←[0 .. 10], x/=5]
</aside>
<aside> 🌟 **Tuples Highlights
→ tuples are immutable, each element is fixed and each index is called a coordinate
→ get the first coordinate of a tuple: fst ('H', "HELLO")
→ get the second coordinate of a tuple: snd ('H', "HELLO")
→ produce a list of pairs: zip [1, 2] [3,4] //[(1, 3) (2, 4)]
</aside>
<aside>
🌟 Functions Highlights
**
→ doc the type on functions: *biggerThan :: Int → Int → Bool
biggerThan x y = x > y
*
→ infix function: *biggerThan x y = (>) x y
→* Lambda: anonymous functions, they can be executed as values and without an explicit context *\\p1 p2 → EXP(p1 p2)
→* High Order Function: it can be passed as parameters or return another function *ev :: (Int -> Int) -> Int
*
- map: takes a function and a list and applies that function to every element in the list, producing a new list *map (+3) [1,5,3,1,6]
-* filter: takes a predicate and a list and then returns the list of elements that satisfy the predicate *filter (>3) [1,5,3,2]
-* foldr: takes a binary function, a starting value (accumulator) and a list to fold up *foldl (\\acc x -> x : acc) []
* **foldl: it does the same, but starts by the left side
*→ Currying: it receives multiply arguments and returns a function's sequence evaluation *sum x y z = x + y + z
**if a parameter was not passed, it returns a function with it
→ Composition: it's a functions chain *(funcA . funcB)
*
→ Function application ($): the expression on its right of $ is applied as the parameter to the function on its left *sum $ map sqrt [1...5]
*
→ Guards (|): a way of testing whether some property of a value (or several of them) are true or false *age | age < underAge = "You're underaged" | otherwise = "You're an adult!" where underAge = 18
*
→ Recursion: a way of defining functions in which the function is applied inside its own definition
→ Polymorphic Function: it cannot see or operate the type variables
</aside>