Why Haskell Matters

Download Why Haskell Matters

Post on 11-May-2015

2.212 views

Category:

Technology

0 download

Embed Size (px)

DESCRIPTION

First presentation of the Vancouver Haskell Meetup. Here we introduce the capabilities of Haskell and some of it's main characteristics as a language

TRANSCRIPT

<ul><li>1.Why Haskell MattersRoman GonzalezVancouver Haskell MeetupOctober 20, 2010 </li></ul> <p>2. So what is this language about? Haskell is a pure, statically typed, lazy functionalprogramming language. Name inspired by mathematician Haskell Brooks Curry Based on a more primitive language called Lambda Calculus There is no iteration or sequences of actions, everything is recursive 3. When learning Haskell is a good idea? If you are a experienced developer that wants to grow professionally by learning completely new and dierent approaches to solve problems If you need to build systems where correctness is critical (Banks, Safety-Critical Systems) Is well suited to implement Domain Specic Languages, so it could work very well in systems programming as Erlang, it is easy to implement parallel and concurrent programs safely due to the pureness of the language 4. Benets of using Haskell Algorithms are normally shorter and more concise than their iterative versions Due to the use of function composability, currycation and high-order functions, code can be reused in really smart ways The kick-ass type system with type inference makes you develop code that would normally work at the rst compilation 5. Properties of Haskell Functions wont perform sequentially, instead they will evaluate expressions when needed (lazily) There is a controlled environment for side eect functions but by default functions are pure (non-side eect). Management of resources is abstracted completely from the developer (GC and automatic allocation of data included) Strong typing makes correctness happen, and most of the the issues regarding this type systems wont get on the way due to the compilers type inference system 6. Quicksort in C void qsort(int a[], int lo, int hi) {int h, l, p, t;if (lo &lt; hi) {l = lo;h = hi;p = a[hi]; do {while ((l &lt; h) &amp;&amp; (a[l] l) &amp;&amp; (a[h] &gt;= p))h = h-1;if (l &lt; h) {t = a[l];a[l] = a[h];a[h] = t;}} while (l &lt; h); a[hi] = a[l];a[l] = p; qsort( a, lo, l-1 );qsort( a, l+1, hi );}} 7. Quicksort in Haskellqsort :: (Ord a) =&gt; [a] -&gt; [a]qsort [] = []qsort (x:xs) = (qsort lower) ++ [x] ++ (qsort upper)wherelower = filter (&lt; x) xsupper = filter (&gt;= x) xs -- qsort [4,5,1,6,3,2]-- First execution:-- x = 4-- xs = [5,1,6,3,2]-- lower = [1,3,2]-- upper = [5,6] 8. Simple Functions One parameter functions Haskell has only one parameter functions inc :: Int -&gt; Int inc x = x + 1isNotZero :: Int -&gt; Bool isNotZero x = x /= 0 9. High Order Functions Functions that returns functionsadd :: Int -&gt; (Int -&gt; Int)add x = y -&gt; x + y compare :: (Eq a) =&gt; a -&gt; (a -&gt; Bool)compare a = b -&gt; a == b -- Syntactic Sugar complements of Haskelladd :: Int -&gt; Int -&gt; Intadd x y = x + y compare :: (Eq a) =&gt; a -&gt; a -&gt; Boolcompare x y = x == y 10. High Order Functions Assign returned functions directlyIf inc is a function of type: Int -&gt; Int and (+1) is a function of type: Int -&gt; Int Then why dont assign it directly? This is called currycationinc = (1 +)isNotZero = (0 /=) 11. High Order Functions Functions that receive functions as parameters Functions can also receive functions as parameters:filter :: (a -&gt; Bool) -&gt; [a] -&gt; [a]filter fn (x:xs)| fn x = x : filter fn xs| otherwise = filter fn xs -- filter isNotZero [1,0,2,0,3]-- =&gt; [1,2,3]They can also be curryed:removeZeros :: [Int] -&gt; [Int]removeZeros = filter isNotZero 12. Lists Denition of a list An empty value: [] A cons value: Item on the left and list on the right, the whole thing being a new list [1,2,3] == (1:2:3:[]) == (1:(2:(3:[])))So we have two basic functions to get the two components of acons:head :: [a] -&gt; ahead (x:_) = x tail :: [a] -&gt; [a]tail (_:xs) = xs 13. Lets dene some functions Simple functions with a common interface sum :: (Num a) =&gt; [a] -&gt; a sum [] = 0 sum (x:xs) = x + sum xsprod :: (Num a) =&gt; [a] -&gt; a prod [] = 1 prod (x:xs) = x * prod xslength :: [a] -&gt; Int length [] = 0 length (x:xs) = 1 + length xs 14. Folding Basicsfoldr :: (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; b foldr fn zero [] = zero foldr fn zero (x:xs) = fn x (foldr fn zero xs){- foldr (+) 0 [3,2,5]- [] is replaced by 0 - (:) function is replaced by the (+) function-&gt; 3 : (2 : (5 : [])) -&gt; 3 + (2 + (5 + 0)) -} 15. Folding Using foldr and partially applied functions-- foldr (+) 0 :: (Num a) =&gt; [a] -&gt; asum = foldr (+) 0 -- foldr (*) 1 :: (Num a) =&gt; [a] -&gt; aprod = foldr (*) 1 -- foldr (+) 0 :: (Num a) =&gt; [a] -&gt; alength = foldr (_ accum -&gt; 1 + accum) 0 16. Folding Insertion Sort insertion :: (Ord a) =&gt; a -&gt; [a] -&gt; [a]insertion a [] = [a]insertion a (b:bs)| a [a] -&gt; [a]insertionSort = foldr insertion [] {-insertionSort [3,2,4]-&gt; insertion 3 (insertion 2 (insertion 4 ([])))-} 17. Function Composition BasicsLets abstract the composition of functions (.) :: (b -&gt; c) -&gt; (a -&gt; b) -&gt; (a -&gt; c) (.) f g = x -&gt; f (g x)Using the (.) function, we can compose function together to create new ones: map :: (a -&gt; b) -&gt; [a] -&gt; [b] map fn = foldr ((:) . fn) [] -- (x:xs) == (:) x xs -- (:) . fn = x -&gt; (:) fn x 18. Lazy Evaluation Innite ListsHaskell is a lazy language, meaning that the language will evaluateexpressions only one needed.iterate :: (a -&gt; a) -&gt; a -&gt; [a]iterate fn a = (a : iterate fn (fn a)) -- [a, fn a, fn (fn a), fn (fn (fn a))), ...]This will do an innite recursion, it will stop when algorithms donot require more values from the innite listsbitValues :: [Int]bitValues = iterate (*2) 1 -- take 8 bitValues -&gt; [1,2,4,8,16,32,64,128] 19. Algebraic Data Types Maybe Data TypeTo handle nullable values we use an Algebraic Data Type called Maybe data Maybe a = Just a| Nothingderiving (Show) Where the a in the Maybe could be any type (Int, String, Char)Examples of values of type Maybe Int Just 20 Nothing 20. Unfoldsunfoldr :: (a -&gt; Maybe (b, a)) -&gt; a -&gt; [b]unfoldr fn x =case fn x ofJust (a, b) -&gt; a : (unfoldr fn b)Nothing -&gt; [] 21. From Int to [Bit]type Bit = Int nextBit :: Int -&gt; Maybe (Bit, Int)nextBit x| x &gt; 0 = Just (m, d)| otherwise = Nothingwhered = x div 2m = x mod 2 toBinary :: Int -&gt; [Bit]toBinary = unfoldr nextBit 22. Binary to Decimal Example of high-order functions, composability and innite listszipWith:: (a -&gt; b -&gt; c) -&gt; [a] -&gt; [b] -&gt; [c]zipWith_ _ [] = []zipWith_ [] _ = []zipWithfn (x:xs) (y:ys) = (fn x y) : zipWith fn xs ys binaryToDecimal :: [Bit] -&gt; IntbinaryToDecimal = sum . zipWith (*) bitValues 23. Why Haskell is not being used as much? Its dierent, people is always afraid of what is dierent Its dicult, mostly related to the rst point Its unpopular, mostly related to the rst and second point Its risky (for employers), there are not many Haskell developers to count on, of course this is mostly related to the rst, second and third point Libraries are broad, but there is no depth (Many incompatible experimental libraries that do the same thing) 24. Thank You! Get started at learnyouahaskell.com Github: http://github.com/roman/haskell meetupTwitter: @romanandreg </p>