Intro to haskell video by paul szulc
world -> (world, a)
An interactive program is a pure function that takes the
current state of the world
as its ARG and produce a modified world as result Protip - Don’t try to track real world state in code. Instead take it as an argument, since only the world knows what state it’s in
Try +set :t
GHC => Glassgow Haskell Compiler / Glorious Haskell Compiler
##Functions:
{-
- helloWorld is a function without parameters
- function without parameter is called definition
- function name starts with small letter
-}
helloWorld = "Hello World"
complexCalc x y z = x * y * z
Prelude> :t complexCalc
complexCalc :: Integer -> Integer -> Integer -> Integer
-- same as
complexCalc :: Integer -> (Integer -> (Integer -> Integer))
Prelude> complexCalc 10 20 30
Prelude> ((complexCalc 10) 20) 30
Polymorphic types
Prelude> :t length
length :: [a] -> Int
-- 'a' is a type variable, which means that length
Prelude> length [10, 20, 30]
3
Prelude> length [False, False, True]
3
Prelude> length ["one", "two", "three"]
3
List101
Prelude> numbers = [1, 3, 5, 7]
-- Gets the element at index 2
Prelude> numbers !! 2
5
-- True if list is empty
Prelude> null numbers
False
-- Gets the first element
Prelude> head numbers
1
-- Gets the last element
Prelude> last numbers
7
-- Delete 2 elements
Prelude> drop 2 numbers
[5,7]
-- Take 2 elements
Prelude> take 2 numbers
[1,3]
-- True if elem is in list
Prelude> elem 5 numbers
True
List101 - List Comprehensions
-- | such as
-- <- is derived from
Prelude> take 5 [2^x | x <- [1..]]
[2,4,8,16,32]
-- Tuples
Prelude> [(x,y) | x <- [1..5], y <- [1..5]]
[(1,1),(1,2),(1,3),(1,4),(1,5),(2,1),(2,2),(2,3),(2,4),(2,5),(3,1),(3,2),(3,3),(3,4),(3,5),(4,1),(4,2),(4,3),(4,4),(4,5),(5,1),(5,2),(5,3),(5,4),(5,5)]
Prelude> zip "abc" (cycle [0,1])
[('a', 0),('b', 1),('c',0)]
Classes
public class Foo() ?? no, those are nor classes but Type classes
Type Classes
Type class is a set of types which support certain collection of functions defined
Prelude> :t read
read :: Read a => String -> a
-- read reads a string and returns a type of the string
Prelude> read "5"
*** Exception: Prelude.read: no parse
-- we need to explicitly state the type
Prelude> read "5" :: Int
5