Created
October 3, 2017 04:45
-
-
Save sharno/a8c4d0f2c650f047be6f67b844e4b404 to your computer and use it in GitHub Desktop.
Lecture notes for Bartosz Milewski's videos on Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:l <filename> -- load | |
:r -- reload | |
:q -- quit | |
:t <expression> -- type | |
:i -- info | |
:k <something> -- kind | |
:sprint <expression> -- prints an expression without evaluation | |
------------------------------------ | |
-- this is a comment | |
sqDist :: Double -> Double -> Double | |
sqDist x y = x^2 + y^2 | |
main :: IO () | |
main = print (sqDist 3 4) | |
------------------------------------ | |
main = True -- won't compile | |
:t print | |
print :: Show a => a -> IO () | |
:t putStrLn | |
putStrLn :: String -> IO () | |
putStrLn (show (sqDist 3 4)) | |
main = putStrLn "Hello World!" | |
------------------------------------ | |
sqDist p = (fst p)^2 + (snd p)^2 -- passing only one argument | |
sqDist (x, y) = x^2 + y^2 -- same as the one before | |
main = print $ sqDist $ (3, 4) -- $ is having the lowest precedence of operators and is right associative | |
-- it's used generally to get rid of parenthesis | |
-- function calling and its arguments is the highest precedence | |
sq x = x * x | |
print $ sq 2 + 3 -- this is 7 | |
print $ sq $ 2 + 3 -- this is 25 | |
-- indentation is important to continue an expression | |
sq (-1) -- parenthesis are important here | |
dist pt = sqrt $ sqDist pt -- pt is a Pair here | |
dist = sqrt . sqDist -- this is same as previous expression. Dot composes functions together into a single one | |
-- precendence of Dot is very high (almost like function application) | |
-- Dot is right associative | |
flop :: (a, b) -> (b, a) | |
flop p = (snd p, fst p) | |
flop' (x, y) = (y, x) | |
sqDist :: Double -> Double -> Double | |
sqDist x y = x^2 + y^2 | |
sqDist 0 -- this is currying which is partial application of a function | |
distFromZero = sqDist 0 -- this is a function now that gets distance of any number from zero | |
:t distFromZero -- Double -> Double | |
-- currying the same function that took only a Pair is not possible | |
------------------------------------ | |
inc x = 1 + x | |
inc = (+) 1 -- this is same as previous line | |
inc = (+ 1) | |
inc = (1 +) -- all are same as they have a section of their arguments missing | |
Void -- is a type which is empty set | |
() -- Unit is the singleton and it's available everywhere | |
data () = () -- the left side is the name of the data and the right side () is the constructor of the data type | |
data Product a b = P a b | |
-- ^type constructor ^data constructor | |
P :: a -> b -> Product a b | |
-- every constructor is like a function as it takes arguments as well | |
-- usually we use the same name for both type and data constructors not to pollute the name space | |
data (,) a b = (,) a b -- this is the constructor for a normal Pair | |
(,) 1 2 -- this equals (1, 2) | |
------------------------------------ | |
fst :: (a, b) -> a | |
fst (x, _) = x | |
snd :: (a, b) -> b | |
snd (_, y) = y | |
:t (,) -- we are requesting for the data constructor not the type constructor (beacuse we are asking for the type) | |
-- ^data constructor | |
(,) :: a -> b -> (a, b) | |
-- ^~~~~ type constructor | |
:k (,) -- we are asking about the kind of type constructor | |
-- ^type constructor | |
(,) :: * -> * -> * -- star means any Haskell type | |
:t seq -- seq causes the first argument to be eagerly evaluated and returns the second argument | |
x = 1 + 2 | |
y = x + 1 | |
seq y () -- this causes y to be evaluated and returns () | |
-- but seq does a shallow evaluation. meaning it just evaluate the top level of this value and doesn't go deeper in the evaluation tree | |
x = 1 + 2 | |
y = x + 1 | |
import Data.Tuple | |
z = swap (x, y) | |
:sprint z -- shows z = _ | |
-- meaning that z is not even evaluated as a tuple | |
seq z () | |
:sprint z -- shows z = (_, _) | |
-- meaning z got evaluated to be a tuple but x and y weren't evaluated | |
-- seq itself doesn't cause evaluation. It causes the first argument to be evaluated (shallow) if the second argment is evaluated | |
-- seq could be then used in parallelism when we want to imitate launching another thread (evaluate first argument) if I exectued the second argument | |
-- it sequences evaluation of arguments by making the first evaluate before the second | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment