Learn You a Haskell for Great Good!

• use parentheses for negative number.
• 5 * (-3) instead of 5 * -3.
• binary operation on same type
• True == 5 is not allow because comparing bool with number
• 5 + “llama”is not allow because comparing number with string
• 5 + 4.0 is ok because the 5 adapted to become a floating-point number

Baby’s first functions

• Infix examples are +, -, *, /, ==, …etc.
• prefix examples are succ, min, … etc.
• function application has highest precedence
• div 92 10 is equivalent to 92 ‘div’ 10
• to load a script (without .hs)
• :l [filename]
• function in Haskell don’t have to be in any particular order
• if then else statement must have an else statement
• ‘ is a valid character for function
• functions can’t begin with uppercase letters
• function without parameter is a definition

An intro to lists

• homogenous (list of elements of the same type)
• let keyword define a name
• lists are denoted by square brackets []
• a string is just a list of characters (“string”, character – ‘c’)
• combine two lists
• list ++list
• the con operator put an element (not a list) at the beginning of the list
• element : list
• [] is an empty list
• get the x element out of the list where indices start at 0.
• list !! x
• list can also contain list
• return the first element of the list, (e.g. head [5,4,3,2,1] gives 5)
• return the same list but without the head, (e.g. tail [5,4,3,2,1] gives [4,3,2,1])
• tail list
• return the last element of the list, (e.g. last [5,4,3,2,1] gives 1)
• last list
• return the same list but without the tail (e.g. init [5,4,3,2,1] gives [5,4,3,2])
• init list
• return the length of the list
• length list
• check if the list is empty
• null list
• return the same list in reverse order
• reverse list
• return the same list using only x number of the elements from the beginning
• take x list
• return the same list without the x number of the elements from the beginning
• drop x list
• return the sum of a list of numbers
• sum list
• return the product of a list of numbers
• product list
• check whether a is in the list
• elem a  list

Texas ranges

• to make a list from a to b
• [a .. b]
• to make a list from a to b with step s (e.g. [3,6..20] gives [3,6,9,12,15,18])
• [a,(a+s)..b]
• to make a infinite list with step s
• [a,(a+s)..]
• takes a list and cycles it into an infinite list
• cycle list
• takes an element and produce an infinite list of just that element
• repeat x
• give a list of x elements of a
• replicate x a

I’m a list comprehension

• a list comprehension that constraint the variable from the criteria before calculating the function of the element into a list. (filtering)
• [function | predicate 1, predicate 2 ..]
• check whether x is odd
• odd x
• _ means we don’t care what we’ll draw from the list

Tuples

• can contain a combination of several types
• use parentheses () to denoted a tuple
• list of tuple will force the list to use the same tuple type
• takes a pair and returns its first component
• fst tuple
• takes a pair and return its second component
• snd tuple
• combine corresponding elements of both list and put them in a tuple and return a list with the same length as the shorter list
• zip list1 list2
• Example: right triangle
• find the dimension of a right triangle with perimeter equal to 24.
• let rightTriangles = [ (a,b,c) | c <-[1..10], b <-[1..c], a <-[1..b], a^2+b^2==c^2, a+b+c==24]

Other commands

• quit ghci
• :quit
• to set default text editor
• :set editor [editor]
• to edit a file
• :edit [filename.hs]