introduction to functional programming in haskell
play

Introduction to Functional Programming in Haskell 1 / 51 Outline - PowerPoint PPT Presentation

Introduction to Functional Programming in Haskell 1 / 51 Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order functions Lazy evaluation How to


  1. Introduction to Functional Programming in Haskell 1 / 51

  2. Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order functions Lazy evaluation How to functional program Functional programming workflow Data types Type classes Type-directed programming Haskell style Refactoring (bonus section) Type inference 2 / 51

  3. Outline Why learn functional programming? The essence of functional programming How to functional program Type inference Why learn functional programming? 3 / 51

  4. Why learn (pure) functional programming? 1. This course: strong correspondence of core concepts to PL theory • abstract syntax can be represented by algebraic data types • denotational semantics can be represented by functions 2. It will make you a better (imperative) programmer • forces you to think recursively and compositionally • forces you to minimize use of state ...essential skills for solving big problems 3. It is the future! • more scalable and parallelizable (MapReduce) • functional features have been added to most mainstream languages • many cool new libraries built around functional paradigm Why learn functional programming? 4 / 51

  5. Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order functions Lazy evaluation How to functional program Type inference The essence of functional programming 5 / 51

  6. What is a (pure) function? A function is pure if: • it always returns the same output for the same inputs • it doesn’t do anything else — no “side effects” In Haskell: whenever we say “function” we mean a pure function ! The essence of functional programming 6 / 51

  7. What are and aren’t functions? Always functions: f ( x ) = x 2 + 2 x + 3 • mathematical functions • encryption and compression algorithms Usually not functions: • C, Python, JavaScript, ... “functions” (procedures) • Java, C#, Ruby, ... methods Haskell only allows you to write (pure) functions! The essence of functional programming 7 / 51

  8. Why procedures/methods aren’t functions • output depends on environment • may perform arbitrary side effects The essence of functional programming 8 / 51

  9. Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order functions Lazy evaluation How to functional program Type inference The essence of functional programming 9 / 51

  10. Getting into the Haskell mindset Haskell Java sum :: [Int] -> Int int sum(List<Int> xs) { sum [] = 0 int s = 0; sum (x:xs) = x + sum xs for (int x : xs) { s = s + x; } return s; In Haskell, “ = ” means is not change to ! } The essence of functional programming 10 / 51

  11. Getting into the Haskell mindset Quicksort in C void qsort(int low, int high) { int i = low, j = high; int pivot = numbers[low + (high-low)/2]; while (i <= j) { while (numbers[i] < pivot) { i++; } while (numbers[j] > pivot) { j--; } if (i <= j) { swap(i, j); i++; j--; } Quicksort in Haskell } if (low < j) qsort(low, j); qsort :: Ord a => [a] -> [a] if (i < high) qsort [] = [] qsort(i, high); qsort (x:xs) = qsort (filter (<= x) xs) } ++ x : qsort (filter (> x) xs) void swap(int i, int j) { int temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; } The essence of functional programming 11 / 51

  12. Referential transparency a.k.a. referent An expression can be replaced by its value without changing the overall program behavior length [1,2,3] + 4 what if length was a Java method? 3 + 4 ⇒ Corollary : an expression can be replaced by any expression with the same value without changing program behavior Supports equational reasoning The essence of functional programming 12 / 51

  13. Equational reasoning Computation is just substitution! sum [2,3,4] sum (2:(3:(4:[]))) ⇒ sum :: [Int] -> Int 2 + sum (3:(4:[])) ⇒ sum [] = 0 2 + 3 + sum (4:[]) sum (x:xs) = x + sum xs ⇒ 2 + 3 + 4 + sum [] ⇒ equations 2 + 3 + 4 + 0 ⇒ 9 ⇒ The essence of functional programming 13 / 51

  14. Describing computations Function definition : a list of equations that relate inputs to output • matched top-to-bottom • applied left-to-right Example: reversing a list ✗ imperative view : how do I rearrange the elements in the list? ✓ functional view : how is a list related to its reversal? reverse :: [a] -> [a] reverse [] = [] reverse (x:xs) = reverse xs ++ [x] The essence of functional programming 14 / 51

  15. Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order functions Lazy evaluation How to functional program Type inference The essence of functional programming 15 / 51

  16. First-order functions Examples • cos :: Float -> Float • even :: Int -> Bool • length :: [a] -> Int The essence of functional programming 16 / 51

  17. Higher-order functions Examples • map :: (a -> b) -> [a] -> [b] • filter :: (a -> Bool) -> [a] -> [a] • (.) :: (b -> c) -> (a -> b) -> a -> c The essence of functional programming 17 / 51

  18. Higher-order functions as control structures map : loop for doing something to each element in a list map f [2,3,4,5] = [f 2, f 3, f 4, f 5] map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs map even [2,3,4,5] = [even 2, even 3, even 4, even 5] = [True,False,True,False] fold : loop for aggregating elements in a list foldr f y [2,3,4] = f 2 (f 3 (f 4 y)) foldr :: (a->b->b) -> b -> [a] -> b foldr f y [] = y foldr f y (x:xs) = f x (foldr f y xs) foldr (+) 0 [2,3,4] = (+) 2 ((+) 3 ((+) 4 0)) = 2 + (3 + (4 + 0)) = 9 The essence of functional programming 18 / 51

  19. Function composition Can create new functions by composing existing functions • apply the second function, then apply the first Function composition (f . g) x = f (g x) (.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x) Types of existing functions Definitions of new functions plus2 = succ . succ not :: Bool -> Bool odd = not . even succ :: Int -> Int second = head . tail even :: Int -> Bool drop2 = tail . tail head :: [a] -> a tail :: [a] -> [a] The essence of functional programming 19 / 51

  20. Currying / partial application In Haskell, functions that take multiple arguments are implicitly higher order Haskell Curry plus :: Int -> Int -> Int Curried plus 2 3 plus :: Int -> Int -> Int Uncurried increment :: Int -> Int plus (2,3) increment = plus 1 plus :: (Int,Int) -> Int a pair of ints The essence of functional programming 20 / 51

  21. Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order functions Lazy evaluation How to functional program Type inference The essence of functional programming 21 / 51

  22. Lazy evaluation Supports: In Haskell, expressions are reduced: • infinite data structures • only when needed • separation of concerns • at most once nats :: [Int] nats = 1 : map (+1) nats fact :: Int -> Int fact n = product (take n nats) min3 :: [Int] -> [Int] What is the running time of this function? min3 = take 3 . sort John Hughes, Why Functional Programming Matters , 1989 The essence of functional programming 22 / 51

  23. Outline Why learn functional programming? The essence of functional programming How to functional program Functional programming workflow Data types Type classes Type-directed programming Haskell style Refactoring (bonus section) Type inference How to functional program 23 / 51

  24. FP workflow (simple) “obsessive compulsive refactoring disorder” How to functional program 24 / 51

  25. FP workflow (detailed) Norman Ramsey, On Teaching “How to Design Programs” , ICFP’14 How to functional program 25 / 51

  26. Outline Why learn functional programming? The essence of functional programming How to functional program Functional programming workflow Data types Type classes Type-directed programming Haskell style Refactoring (bonus section) Type inference How to functional program 26 / 51

  27. Algebraic data types Data type definition Definitions consists of ... • introduces new type of value • a type name • enumerates ways to construct • a list of data constructors values of this type with argument types Some example data types Definition is inductive • the arguments may recursively data Bool = True | False include the type being defined data Nat = Zero | Succ Nat • the constructors are the only way data Tree = Node Int Tree Tree to build values of this type | Leaf Int How to functional program 27 / 51

  28. Anatomy of a data type definition type name data Expr = Lit Int cases | Plus Expr Expr types of arguments data constructor Example: 2 + 3 + 4 Plus (Lit 2) (Plus (Lit 3) (Lit 4)) How to functional program 28 / 51

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend