Haskell Flashcards
let
Defines a function
let original = 2
let add x y = x + y
Other way:
let a = 1 in a + a
let a = 2; b = 4 in a * b
map
Map is a name of a higher-order function that applies a given function to each element of the list, returning a list of results.
let number = [2, 4 , 6]
map (+3) numbers = [5, 7, 9]
map (\x -> x*x) [1..5]
map function list
Filter
Filter is the name of a higher-order function that processes a data structure, typically a list, in some order to produce a new data structure containing those exact elements of the original data structure that match a given condition.
let numbers = [25,9,(-20),100,16,(-2)]
filter (<16) numbers = shows numbers below 16
filter f (x:xs)
|f x = x : rest
|otherwise = rest
where rest = filter f xs
square_even :: [Int] -> [Int]
square_even list = map (^2) (filter even list)
Fold
Reduce/Fold is the name of a higher-order function which reduces a list of values to a single value by repeatedly applying a combining function to the list values.
numbers = [2, 4, 6]
foldl (+) 0 numbers = adds all numbers including 0 from left to right.
sum’’ list = foldr (\ x acc -> acc + x) 0 list
Acc:
Actuator -> acc = 0 , then becomes 0 + whatever is on the right of the list
foldr1 = uses the last element in the list as the accumulator
foldr1 cannot change the type of the list
Side Effect
A side effects is anything that changes global state. This means that is could change a variable, a console etc, in which pure functions disallow things like prints, loops and variables. No control flow either, like if statements, because that makes the result indeterministic.
Pure Function
A functions with NO SIDE EFFECTS, meaning it is DETERMINISTIC and ALWAYS RETURNS A VALUE.
Subroutine
They predominately have side effects. These subroutines that have side effects cause issues for code refactoring, complier designers, optimization and parrelization.
**
Float (square)
Integer (square)
&&
||
AND statement
OR statement
Not
Gives opposite for boolean
==
Equality testing
/=
Not equal
Negatives
Must be in brackets
Priority
Functions
mod
Remainder
``
Allows function to become infix
Brackets
Allows add and sub to become prefix
Succ
Successor
::
Has type
:t True
True :: Bool
– and {–}
Allows comments
Show
Turn anything into a string
putStrLn
Prints
If
If True then 1 else 0, both branches must exist.
Lists
Contain items that all have the same type.
[]
They are linked (go through everything from the beginning)
Last, Init, Head, Tail
: = puts new head on (1: [2,3,4,5])
!! = index of list
Tuples
Allows us to bind two or move values together
Can contain different types
()
Fixed length
Range
[1..10]
Step Size
[2,4..20]
Would give = [2,4,6,8,10 etc]
Counting downwards always needs a step size.
take 4 [1..]
Infinite list, but returns the first four.
Repeat and Cycle
Repeats the input and repeats the list.
List Comprehension
Can produce more complex lists:
[ 2*x |x <- [1,2,3,4]] = [2,4,6,8]
Predicates
[2*x | x <- [1,2,3,4], x<2] = [2]
Using commas to filter results.
List Comprehension : Upper Case
st [c | c <- st, c ‘elem’ [‘A’…‘Z’]]
List Comprehension : Prime Numbers With Factorials
factors n = [x | x <- [1..n], n mod
x == 0]
prime n = [x | x <- [1..n], length (factors x) == 2] (only got 2 factors)