An introduction to functional programming July 23, 2010 About
Outline About About
What is functional programming? What is? Why functional programming? Why? is novel. is powerful. is fun. About A brief description of the history of.
is a purely functional programming language. Referential transparency. is lazy. Programs as a series of transformations on data. is statically typed. uses a very good type system that has type inference. is elegant and concise. was made by some really smart guys. About
About Learning is much like learning to program for the first time it s fun! It forces you to think differently.
About Functional programming Functional programming is a style of programming which models computations as the evaluation of expressions. Functional programming means that programs are executed by evaluating expressions. Brevity, ease of understanding, no core dumps. Code reuse, strong glue, powerful abstractions, built-in memory management. Purity (immutable data, referential transparency, lazy evaluation, purity, and side effects). Recursion. About
About is an advanced purely functional programming language. It is a polymorphically statically typed, lazy, purely functional programming language. It is an open-source product of more than twenty years of cutting-edge research. It allows rapid development of robust, concise, correct software. With strong support for integration with other languages, built-in concurrency and parallelism, debugger, profilers, rich libraries and an active community, makes it easier to produce flexible, maintainable high-quality software. About
About The Glasgow Compiler (GHC) The Glorious Glasgow Compilation System, also known as the Glasgow Compiler (GHC), is a state-of-the-art, open source, compiler and interactive environment for the functional language. GHC has two main components: GHCi is an interactive interpreter and debugger. GHC is an optimizing compiler. About The Glasgow Compiler (GHC). http://www.haskell.org/ghc/
About The Platform The Platform is a comprehensive, robust development environment for programming in. The platform makes it trivial to get up and running with a full development environment. The platform provides a comprehensive, standard base for commercial and open source development that maximises interoperability and stability. The platform is comprehensive, robust, and cutting-edge. About The Platform. The standard development environment. http://hackage.haskell.org/platform/
About The community The Communities and Activities Report. in industry. in education. in research. and mathematics. Planet, The Monad.Reader..... http://www.haskell.org/ About
About Frag About
About About http://www.haskell.org/
Example () 1. ghci> "" "" 2. ghci> putstrln "" 3. ghci> putstr "\n" 4. main :: IO () main = putstrln "" About
Factorial Example (Factorial) factorial :: (Integral a) => a -> a 1. factorial n = if n == 0 then 1 else n * factorial (n - 1) 2. factorial n n == 0 = 1 otherwise = n * factorial (n - 1) 3. factorial 0 = 1 factorial n = n * factorial (n - 1) 4. ghci> let factorial n = product [1..n] ghci> factorial 32 263130836933693530167218012160000000 About
About
Basic interaction Examples (Simple arithmetic) 1. ghci> 19 + 88 107 2. ghci> (*) 19 88 1672 3. ghci> 19 + - 88 Precedence parsing error 4. ghci> :set +t ghci> 19 + (- 88) -69 it :: Integer ghci> :unset +t About
Basic interaction Examples (Boolean logic) 1. ghci> True && (False True) True 2. ghci> True && 1 No instance for... Examples (Value comparisons) 1. ghci> (20 / 10) == (2 * 4) False 2. ghci> (2 ^ 3) <= 16 True 3. ghci> not ("Hello" /= "Hello") True About
Basic interaction Examples (Precedence and associativity) 1. ghci> (50 * 100) - 4999 1 ghci> 50 * 100-4999 1 ghci> 50 * (100-4999) -244950 2. ghci> :info (+) class (Eq a, Show a) => Num a where (+) :: a -> a -> a... infixl 6 + About
Basic interaction Examples (Undefined values) 1. ghci> pi 3.141592653589793 2. ghci> e... Not in scope... Example (Introducing variables) About ghci> let e = exp 1 ghci> e 2.718281828459045
Lists Examples (Lists) 1. ghci> ["David","Roger",[]] ["David","Roger",""] 2. ghci> [True,False,"True"] Couldn t match expected type... 3. ghci> [1..5] ++ [5,4..1] [1,2,3,4,5,5,4,3,2,1] 4. ghci> [1.0,1.25..2.0] [1.0,1.25,1.5,1.75,2.0] 5. ghci> [1.0..1.8] [1.0,2.0] About
Lists Examples (Operators on lists) 1. ghci> ["David","Roger"] ++ ["Richard"] ["David","Roger","Richard"] 2. ghci> [] ++ [False,True] ++ [True] [False,True,True] 3. ghci> 1:[2,3] [1,2,3] 4. ghci> 1:[] [1] 5. ghci> [1,2]:[] No instance for... About
List comprehensions Examples 1. ghci> [x*2 x <- [1..10]] [2,4,6,8,10,12,14,16,18,20] 2. pairs :: [a] -> [b] -> [(a,b)] pairs xs ys = [(x,y) x <- xs, y <- ys] 3. perms :: (Eq a) => [a] -> [[a]] perms [] = [[]] perms xs = [x:ps x <- xs, ps <- perms (xs\\[x])] About
Strings and characters Examples (Strings and characters) 1. ghci> "Lost for Words" "Lost for Words" 2. ghci> putstrln "The Division\nBell" The Division Bell 3. ghci> [ A, n, i, m, a, l, s ] "Animals" 4. ghci> "" == [] True 5. ghci> D :("avid" ++ " Gilmour" ++ "") "David Gilmour" About
Example Example (LineCount.hs) main :: IO () main = interact linecount where linecount input = show (length (lines input)) ++ "\n" About runghc LineCount < LineCount.hs 4
Lazy evaluation Lazy evaluation causes that expressions are not evaluated when they are bound to variables, but their evaluation is deferred until their results are needed by other computations. A lazy evaluator will only evaluate an argument to a function if that argument value is needed to compute the overall result. If an argument is structured (a list or a tuple, for instance), only those parts of the argument which are needed will be examined. About
Lazy evaluation Examples (Infinite lists) 1. ones :: [Int] ones = 1 : ones 2. powers :: Int -> [Int] powers n = [n^x x <- [0..]] 3. pythagtriples :: [(Int,Int,Int)] pythagtriples = [(x,y,z) z <- [2..], y <- [2..z - 1], x <- [2..y - 1], x*x + y*y == z*z] About
Lazy evaluation Example (The Sieve of Eratosthenes) primes :: [Integer] primes = sieve [2..] sieve :: [Integer] -> [Integer] sieve (x:xs) = x : sieve [y y <- xs, y mod x > 0] memberord :: (Ord a) => [a] -> a -> Bool memberord (x:xs) n x < n = memberord xs n x == n = True otherwise = False About
Algebraic types Algebraic data type definitions are introduced by the keyword data, followed by the name of the type, an equals sign and then the constructors of the type being defined. The simplest sort of algebraic type is defined by enumerating the elements of the type. Examples About 1. data Temp = Cold Hot 2. data Season = Spring Summer... 3. data Bool = True False
Algebraic types About Examples 1. type Name = String type Age = Int data Person = Person Name Age 2. data Shape = Circle Float Rectangle Float Float deriving (Eq,Ord,Show,Read)
Pattern matching Example (Numerical expressions) data Expr = Lit Int Add Expr Expr Sub Expr Expr eval :: Expr -> Int eval (Lit n) = n eval (Add e1 e2) = (eval e1) + (eval e2) eval (Sub e1 e2) = (eval e1) - (eval e2) About
About
Polymorphism A value is polymorphic if, depending on the context where it is used, it can take more than one type. Example (Last) ghci> :type last last :: [a] -> a ghci> last [1,9,8,9] 9 ghci> last "Ummagumma" a About Parametric polymorphism is when a function s type signature allows various arguments to take on arbitrary types, but the types must be related to each other in some way.
Polymorphism Ad-hoc polymorphism (or overloading) is when the possible types are limited and must be individually specified before use. Example (Show) ghci> :type show show :: (Show a) => a -> String ghci> show 22 "22" ghci> show 2.2 "2.2" About In, ad-hoc polymorphism is achieved by type classes.
Higher order functions A higher order function is a function that takes other functions as arguments or returns a function as result. Example (Map) ghci> :type map map :: (a -> b) -> [a] -> [b] ghci> map (*2) [1..5] [2,4,6,8,10] About Functions as arguments and functions as values. Partial function application and currying. Anonymous (lambda) functions....
1. Understanding the problem. 2. Designing the program. 3. Writing the program. 4. Looking back. Simon Thompson. Where do I begin? A problem solving approach in teaching functional programming. About
Example (The maximum of three integers) 1. Understanding the problem. maxthree :: Int -> Int -> Int -> Int 2. Designing and writing the program. maxtwo :: Int -> Int -> Int maxtwo a b a <= b = b otherwise = a maxthree a b c b <= a && c <= a = a a <= b && c <= b = b otherwise = c maxthree a b c = maxtwo (maxtwo a b) c 3. Looking back. About
Learn you a for great good! About
Learn you a for great good! 1. Read chapters 1-6 of the tutorial Learn You a for Great Good! 2. Answer the following questions. What do you like/dislike about functional programming? What do you like/dislike about? What do you think of the tutorial?... About Miran Lipovača. Learn You a for Great Good! http://learnyouahaskell.com/
Real world 1. Read chapters 1-4 of the book Real World. 2. Answer the following questions. What do you like/dislike about functional programming? What do you like/dislike about? What do you think of the book?... Bryan O Sullivan, Don Stewart, and John Goerzen. Real World. O Reilly Media, 2008. http://book.realworldhaskell.org/ About
and functional programming. Thinking in. A change in perspective. Learning. The standard libraries. The community. Outlook Modules, classes, input and output, higher order functions, testing, monads, data structures, libraries.... About
Bryan O Sullivan, Don Stewart, and John Goerzen. Real World. O Reilly Media, 2008. Simon Thompson. : The Craft of Functional Programming. Addison-Wesley, 1999. Miran Lipovača. Learn You a for Great Good! About