2016
1-1: Introduction
Everything in Haskell is PURE (function), including side effects (print, I/O such as open files, update data, …)
“Pure”: f (x) = a, regardless of ‘x’ value may change, always returns the result ‘a’
https://youtu.be/N6sOMGYsvFA
2.1 Function
f a b = function f, arg a & b
sqDist (x, y ) = x^2 + y^2
main = print $ sqDist (3, 4)
dist pt = sqrt $ sqDist pt
dist = sqrt . sqDist
Note: pronounce “.” as “AFTER”
id :: a -> a [Signature] # Polymorphic
id x = x [Implementation]
flop :: (a, b) -> (b, a)
flop p = = (snd p,fst p)
flop’ (x,y) = (y,x)
Example:
print $ flop (5, “Hello!”)
Currying
sqDist :: Double ->Double -> Double
Equivalent to: sqDist 1 arg but return a function (Double -> Double)
sqDist :: Double -> (Double -> Double)
sqDist x y = x^2 + y^2
Equiv: sqDist (x,y) # but difficult to partial application.
Partial Application (pass fewer args)
sqDist 0 = Double -> Double
sqdistfromzero :: Double -> Double
sqdistfromzero = sqDist 0
2.2 Functions
Function (+) :: D -> D -> D
inc x = 1 + x ~ (+) :: 1 + x
Section (Partial appl) : inc = (+ 1)
To be continued … (Part 2)
Parallel and Concurrent Haskell: http://www.youtube.com/playlist?list=PLbgaMIhjbmEm_51-HWv9BQUXcmHYtl4sw
<b>Read Free Online Book:</b>
http://chimera.labs.oreilly.com/books/1230000000929/index.html
<a href=”https://tomcircle.wordpress.com/wp-content/uploads/2017/07/20170712_200456.png”><img src=”https://tomcircle.wordpress.com/wp-content/uploads/2017/07/20170712_200456.png” alt=”” class=”wp-image-13955 alignnone size-full” width=”1064″ height=”1262″></a>