Haskell is a functional programming language, everything in it is immutable.
- doesn’t execute more than needed
Intro to haskell <- Summary of the youtube lesson
My github for UNSW haskell course
Table of Content
### Commands
##Load
:l
##Run
:r
##Quit
:q
##get DataType
:t
##Comment
-- comments
##Multi Comments
{- Multiline Comments -}
##Datatype
#-- Int -2^63 2^63
maxInt = maxBound :: Int
minInt = minCound :: Int
#-- Interger unbounded whole number (as big as memory)
#-- Float single precision
#-- Double 11 points percision
#-- Bool True False
#-- Char ''
#-- String [Char]
#-- Tuple 2 values
##Basic Arithmethic
add = x + y
sub = x - y
mult = x * y
div = x / y
mod = mod 5 4
-- or
mod = 5 `mod` 4 (infix operator)
##Built in Math function
{-
If define an Int must use fromIntergral to use with sqrt
:t sqrt shows that it returns a float
-}
num9 = 9 :: Int
sqrt9 = sqrt (fromIntergral num9)
piValue = pi
exp9 = exp 9
log9 = log 9
squared9 = 9 ** 2
truncate9 = truncate 9.9
round9 = round 9.4
ceiling9 = ceiling 8.1
floor9 = floor 9.9
{-
- sin, cos, tan, asin, atan, acos, sinh, tanh, cosh, asinh, atanh, acosh
-}
##Logic Operator
trueAndFalse = True && False
trueOrFalse = True || False
notTrue = not(True)
negateMinus1 = negate(-1)
##Lists (singly linked list can only add to the dront)
-- Lists store many elements of the same type
primeNumbers = [3,5,7,11]
-- Concatenate lists (slow if large list)
morePrimes = primeNUmbers ++ [13,17,19,23,29]
-- Colon operator to construct a list
nums = 2 : 7 : 21 : []
-- List of lists
multiList = [ [3,5,7], [11,13,17] ]
-- Quick way to add value to the front of a list
morePrimes2 = 2 : morePrimes
-- Get number of elements in the list
lenPrimes = length <list>
-- Reverse the list
revPrimes = reverse <list>
-- True if list empty
isListEmpty = null <list>
##List Transversal
-- Get first element (if list is empty it will crash)
firstElement = head <list>
-- Get last element
lastElement = last <list>
-- Get element in index 1
secondPrime = <list> !! 1
-- Get everything but the first
listTail = tail <list>
-- Get everything but the but the last
listInit = init <list>
-- Get specified value from list
newList = take <value> <list>
-- Return list after removing specified value
newList = drop <value> <list>
-- Check if value in list (returns Bool)
inList = <value> `elem` <list>
-- Get max value
maxValue = maximum <list>
-- Get minimum value
minValue = minimum <list>
-- Sum values in list (only works on numbers)
sumList = sum <list>
-- Product values in list
multiList = product <list>
## List Comprehension
-- Create list from 0 to 10
zeroToTen = [0..10]
-- Create list of evens by defining the step between the first 2 values
evenList = [2,4..20]
{-
- Repeat repeats a value n times
-}
2s = take <n> (repeat <value>)
-- OR
3s = replicate <n> <value>
-- Cycle replicates values in a list
cycleList = take 10 (cycle <list>)
{-
- Perform operations on all values in a list
- Cycle throught the list
-}
listTimes2 = [x * 2 | x <- <list>]
-- filter the results with conditions
listFiltered = [x * 2 | x <- <list>, x * 2 >= 10]
-- OR
listFiltered = filter (<condition>) <list>
-- More than one conditions
listFiltered = [x | x <- <list>, <condition 1>, <condition 2>]
-- Sort a list
sortedList = sort <list>
-- ZipWith combine lists using a function (according to index)
sumof2List = zipWith (+) [1,2,3] [4,5,6]
>> [5,7,9]
-- takeWhile return list items till condition is false
evensUpTo20 = takeWhile (<condition>) <list>
{-
- foldl operation on each item of a list
- foldr operation from the right
-}
multiList = foldl (*) 2 <list>
## Tuples
-- Stores list of multiple data types but has a fixed size
bobSmith = ("Bob Smith",20)
-- Get first value
bobName = fst bobSmith
-- Get second value
bobAge = snd bobSmith
-- Zip combine values into tuple pairs
names = ["bob", "UxU"]
age = [20, 22]
nameAges = zip names age
## Functions
{-
- ghc make compiles program and executes the main function
- Functions must start with lowercase
- Can define functions and values in ghci with let
- >> let num7 = 7
- >> let getTriple x = x * 3
- main is a function called in the terminal with main
-}
-- define a type declaration for functions
-- functionName :: param1 -> param2 -> returnType
add2Numbers :: Int -> Int -> Int
-- Recursive Function
factorial 0 = 1
factotial n = n * factorial (n - 1)
## simple haskell program
main = do
-- Prints string with new line
putStrLn "Enter your name: "
-- Gets user input and stores it in name
-- <- gets name entered from an IO action
name <- getLine
putStrLn("Hello " ++ name)
## Conditional Statement
isOdd :: Int -> Bool
isOdd n
-- if mod n 2 equals 0 not odd
| mod n 2 == 0 = False
-- else return true
| otherwise = true
-- OR can be shortened to
isEven n = n `mod` 2 == 0
-- Comparison Operators : < > <= >= == /=
-- Logical Operatiors : && || not negate
-- Every if statement must contan an else
doubleEvenNUmber x =
if (mod x 2 /= 0) then x
else x * 2
-- Case statements
getClass :: Int -> String
getClass n = case n of
1 -> "Go to Kindergarten"
2 -> "Go to elementary school"
_ -> "Go somewhere else"
## Lambda
{-
- Create functions without a name
- \ represents lambda then you hvae the args -> and result
-}
1To10Times2 = map (\x -> x * 2) [1..10]