├── .gitignore ├── LICENSE ├── README.md ├── examples-exercises.hs ├── examples-solutions.hs ├── figures ├── MIL_Logo_BW.png ├── diffRegion1.ai ├── diffRegion1.png ├── diffRegion2.ai ├── diffRegion2.png ├── intersectionRegion1.ai ├── intersectionRegion1.png ├── intersectionRegion2.ai ├── intersectionRegion2.png ├── invertedRegion.ai ├── invertedRegion.png ├── region.ai ├── region.png ├── shiftedRegion.ai ├── shiftedRegion.png ├── unionRegion1.ai ├── unionRegion1.png ├── unionRegion2.ai └── unionRegion2.png ├── presentation.pdf └── presentation.tex /.gitignore: -------------------------------------------------------------------------------- 1 | ## Core latex/pdflatex auxiliary files: 2 | *.aux 3 | *.lof 4 | *.log 5 | *.lot 6 | *.fls 7 | *.out 8 | *.toc 9 | *.fmt 10 | 11 | ## Intermediate documents: 12 | *.dvi 13 | *-converted-to.* 14 | # these rules might exclude image files for figures etc. 15 | # *.ps 16 | # *.eps 17 | # *.pdf 18 | 19 | ## Bibliography auxiliary files (bibtex/biblatex/biber): 20 | *.bbl 21 | *.bcf 22 | *.blg 23 | *-blx.aux 24 | *-blx.bib 25 | *.brf 26 | *.run.xml 27 | 28 | ## Build tool auxiliary files: 29 | *.fdb_latexmk 30 | *.synctex 31 | *.synctex.gz 32 | *.synctex.gz(busy) 33 | *.pdfsync 34 | 35 | ## Auxiliary and intermediate files from other packages: 36 | # algorithms 37 | *.alg 38 | *.loa 39 | 40 | # achemso 41 | acs-*.bib 42 | 43 | # amsthm 44 | *.thm 45 | 46 | # beamer 47 | *.nav 48 | *.snm 49 | *.vrb 50 | 51 | # cprotect 52 | *.cpt 53 | 54 | #(e)ledmac/(e)ledpar 55 | *.end 56 | *.[1-9] 57 | *.[1-9][0-9] 58 | *.[1-9][0-9][0-9] 59 | *.[1-9]R 60 | *.[1-9][0-9]R 61 | *.[1-9][0-9][0-9]R 62 | *.eledsec[1-9] 63 | *.eledsec[1-9]R 64 | *.eledsec[1-9][0-9] 65 | *.eledsec[1-9][0-9]R 66 | *.eledsec[1-9][0-9][0-9] 67 | *.eledsec[1-9][0-9][0-9]R 68 | 69 | # glossaries 70 | *.acn 71 | *.acr 72 | *.glg 73 | *.glo 74 | *.gls 75 | 76 | # gnuplottex 77 | *-gnuplottex-* 78 | 79 | # hyperref 80 | *.brf 81 | 82 | # knitr 83 | *-concordance.tex 84 | *.tikz 85 | *-tikzDictionary 86 | 87 | # listings 88 | *.lol 89 | 90 | # makeidx 91 | *.idx 92 | *.ilg 93 | *.ind 94 | *.ist 95 | 96 | # minitoc 97 | *.maf 98 | *.mtc 99 | *.mtc[0-9] 100 | *.mtc[1-9][0-9] 101 | 102 | # minted 103 | _minted* 104 | *.pyg 105 | 106 | # morewrites 107 | *.mw 108 | 109 | # mylatexformat 110 | *.fmt 111 | 112 | # nomencl 113 | *.nlo 114 | 115 | # sagetex 116 | *.sagetex.sage 117 | *.sagetex.py 118 | *.sagetex.scmd 119 | 120 | # sympy 121 | *.sout 122 | *.sympy 123 | sympy-plots-for-*.tex/ 124 | 125 | # pdfcomment 126 | *.upa 127 | *.upb 128 | 129 | #pythontex 130 | *.pytxcode 131 | pythontex-files-*/ 132 | 133 | # Texpad 134 | .texpadtmp 135 | 136 | # TikZ & PGF 137 | *.dpth 138 | *.md5 139 | *.auxlock 140 | 141 | # todonotes 142 | *.tdo 143 | 144 | # xindy 145 | *.xdy 146 | 147 | # xypic precompiled matrices 148 | *.xyc 149 | 150 | # WinEdt 151 | *.bak 152 | *.sav 153 | 154 | # endfloat 155 | *.ttt 156 | *.fff 157 | 158 | # Latexian 159 | TSWLatexianTemp* 160 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Glenn R. Fisher 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # An Introduction to Functional Programming 2 | 3 | This presentation provides an introduction to functional programming. It aims to develop intuition behind the big ideas of functional programming and includes several examples in Haskell. Since the best way to learn is by doing, there are also some exercises to help you start thinking like a functional programmer. 4 | 5 | If you would like to learn more about functional programming, I highly recommend UPenn's CIS 194 class (linked below). I have also included a number of additional links that you may find interesting. 6 | 7 | ## Haskell: Online Classes 8 | 9 | - [UPenn CIS 194: Introduction to Haskell](http://www.seas.upenn.edu/%7Ecis194/spring13/) 10 | - [NICTA Functional Programming Course](https://github.com/NICTA/course) 11 | - [Stanford CS240h: Functional Systems in Haskell](http://www.scs.stanford.edu/14sp-cs240h/) 12 | - [edX: Introduction to Functional Programming](https://www.edx.org/course/introduction-functional-programming-delftx-fp101x-0) 13 | 14 | ## Haskell: Books 15 | 16 | - [*Learn You a Haskell for Great Good!*](http://learnyouahaskell.com) 17 | - [*Real World Haskell*](http://book.realworldhaskell.org) 18 | - [*Haskell Programming from First Principles*](http://haskellbook.com) 19 | - [*Wikibooks: Haskell*](https://en.wikibooks.org/wiki/Haskell) 20 | 21 | ## Scala: Online Classes and Books 22 | - [Coursera: Functional Programming Principles in Scala](https://www.coursera.org/course/progfun) 23 | - [*Functional Programming in Scala*](https://www.manning.com/books/functional-programming-in-scala) 24 | - [Fundtional Programming in Scala Repository](https://github.com/fpinscala/fpinscala) 25 | 26 | ## Functional Programming: Papers 27 | 28 | - [Organizing Programs Without Classes](http://cs.au.dk/~hosc/local/LaSC-4-3-pp223-242.pdf) 29 | - [Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire](http://eprints.eemcs.utwente.nl/7281/01/db-utwente-40501F46.pdf) 30 | - [Equal Rights for Functional Objects or, The More Things Change, The More They Are the Same](equal-rights-for-functional-objects.pdf) 31 | - [Optimal Purely Functional Priority Queues](optimal-purely-functional-priority-queues.pdf) 32 | - [Why Functional Programming Matters](why-functional-programming-matters.pdf) 33 | - [Backtracking Iterators](https://www.lri.fr/~filliatr/publis/enum2.pdf) 34 | - [Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design](http://www.cs.tufts.edu/~nr/cs257/archive/chris-okasaki/breadth-first.pdf) 35 | - [Concatenative Programming: An Overlooked Paradigm in Functional Programming](https://github.com/dterei/Research-Papers/blob/master/To%20Read/CONCATENATIVE%20PROGRAMMING%0AAn%20Overlooked%20Paradigm%20in%20Functional%20Programming.pdf) 36 | - [Crossing the Gap from Imperative to Functional Programming through Refactoring](http://dig.cs.illinois.edu/papers/lambdaRefactoring.pdf) 37 | - [Universality and Expressiveness of Fold](www.cs.nott.ac.uk/~pszgmh/fold.pdf) 38 | 39 | ## Functional Programming: Talks 40 | 41 | - [Functional Principles for Object-Oriented Developers](http://www.youtube.com/watch?v=pMGY9ViIGNU) by Jessica Kerr [51:13] 42 | - [Adventures in Functional Programming](https://vimeo.com/45140590) by Jim Weirich [50:34] 43 | - [Deconstructing Functional Programming](http://www.infoq.com/presentations/functional-pros-cons) by Gilad Bracha [48:33] 44 | - [Living in a Post-Functional World](http://www.infoq.com/presentations/post-functional-scala-clojure-haskell) by Daniel Spiewak [45:46] 45 | - [Extreme Cleverness: Functional Data Structures in Scala](https://www.youtube.com/watch?v=pNhBQJN44YQ) by Daniel Spiewak [39:24] 46 | - [Tangible Functional Programming](https://www.youtube.com/watch?v=faJ8N0giqzw) by Conal Elliott [56:24] 47 | - [Faith, Evolution, and Programming Languages](https://www.youtube.com/watch?v=8frGknO8rIg) by Phillip Wadler [1:06:53] 48 | - [Don't fear the Monad](https://www.youtube.com/watch?v=ZhuHCtR3xq8) by Dr. Brian Beckman [1:07:10] 49 | - [Functional Programming Design Patterns](https://skillsmatter.com/skillscasts/6120-functional-programming-design-patterns-with-scott-wlaschin) by Scott Wlaschin [1:49:10] 50 | - [Domain modelling with the F# type system](http://vimeo.com/97507575) by Scott Wlaschin [1:03:40] 51 | - [Propositions as Types](https://www.youtube.com/watch?v=IOiZatlZtGU) by Philip Wadler [42:42] 52 | - [Functional is cool, but do you know OO](http://www.parleys.com/play/51aa0172e4b01033a7e4b67a/) by Sandro Mancuso [54:41] 53 | - [Stop Writing Classes](http://pyvideo.org/video/880/stop-writing-classes) by Jack Diederich [27:29] 54 | 55 | ## Functional Programming: Algorithms and Data Structures 56 | 57 | - [*Purely Functional Data Structures*](http://www.amazon.com/Purely-Functional-Structures-Chris-Okasaki/dp/0521663504) by Chris Okasaki 58 | - ["What's new in purely functional data structures since Okasaki?"](http://cstheory.stackexchange.com/questions/1539/whats-new-in-purely-functional-data-structures-since-okasaki) 59 | -------------------------------------------------------------------------------- /examples-exercises.hs: -------------------------------------------------------------------------------- 1 | {------------------------------------------------------------------------------- 2 | Rapid Introduction to Haskell: Variables 3 | ------------------------------------------------------------------------------} 4 | 5 | x :: Integer 6 | x = 3 7 | 8 | pi :: Double 9 | pi = 3.1415926 10 | 11 | b1, b2 :: Bool 12 | b1 = True 13 | b2 = False 14 | 15 | {------------------------------------------------------------------------------- 16 | Rapid Introduction to Haskell: Arithmetic 17 | ------------------------------------------------------------------------------} 18 | 19 | ex01 = 3 + 2 20 | ex02 = 19 - 27 21 | ex03 = 2.35 * 8.6 22 | ex04 = 8.7 / 3.1 23 | ex05 = div 12 5 24 | ex06 = 7 ^ 222 25 | 26 | {------------------------------------------------------------------------------- 27 | Rapid Introduction to Haskell: Boolean Logic 28 | ------------------------------------------------------------------------------} 29 | 30 | ex07 = True && False 31 | ex08 = not (True || False) 32 | ex09 = ('a' == 'a') 33 | ex10 = (16 /= 3) 34 | ex11 = "Haskell" > "C++" 35 | 36 | {------------------------------------------------------------------------------- 37 | Rapid Introduction to Haskell: Functions 38 | ------------------------------------------------------------------------------} 39 | 40 | doubleMe :: Integer -> Integer 41 | doubleMe x = x + x 42 | 43 | quadrupleMe :: Integer -> Integer 44 | quadrupleMe x = doubleMe (doubleMe x) 45 | 46 | hypotenuse :: Double -> Double -> Double 47 | hypotenuse length width = sqrt squaredHypotenuse 48 | where squaredHypotenuse = length^2 + width^2 49 | 50 | hypotenuse2 :: Double -> Double -> Double 51 | hypotenuse2 length width = 52 | let squaredHypotenuse = length^2 + width^2 in 53 | sqrt squaredHypotenuse 54 | 55 | {------------------------------------------------------------------------------- 56 | Rapid Introduction to Haskell: Types 57 | ------------------------------------------------------------------------------} 58 | 59 | {- Position -} 60 | 61 | {- Note: To avoid multiple declarations of Position, 62 | we declare and use the type Position1 below. -} 63 | 64 | type Position1 = (Double, Double) 65 | 66 | position :: Position1 67 | position = (1.25, 2.75) 68 | 69 | {- Note: To avoid multiple declarations of magnitude, 70 | we declare and function magnitude1 below. -} 71 | 72 | magnitude1 :: Position1 -> Double 73 | magnitude1 (x, y) = sqrt (x * x + y * y) 74 | 75 | {- Person -} 76 | 77 | {- A Name is a type synonym for a String. -} 78 | type Name = String 79 | 80 | {- An Age is a type synonym for an Integer. -} 81 | type Age = Integer 82 | 83 | {- A Color is either Red, Green, or Blue. -} 84 | type Color = String 85 | 86 | {- A Person has a name, age, and favorite color. -} 87 | data Person = Person Name Age Color 88 | 89 | glenn :: Person 90 | glenn = Person "Glenn R. Fisher" 22 "Green" 91 | 92 | favoriteColor :: Person -> Color 93 | favoriteColor (Person name age color) = color 94 | 95 | -- favoriteColor glenn == Green 96 | 97 | {------------------------------------------------------------------------------- 98 | Example: Position 99 | ------------------------------------------------------------------------------} 100 | 101 | {- a Distance is a length in space -} 102 | type Distance = Double 103 | 104 | {- a Position is a pair of x and y distances -} 105 | type Position = (Distance, Distance) 106 | 107 | {- reflect returns a new Position reflected over the origin -} 108 | reflect :: Position -> Position 109 | reflect (x, y) = (-x, -y) 110 | 111 | {- magnitude returns a Position's distance from the origin -} 112 | magnitude :: Position -> Distance 113 | magnitude (x, y) = sqrt (x * x + y * y) 114 | 115 | {- translate returns a new Position translated by an offset -} 116 | translate :: Position -> Position -> Position 117 | translate (x, y) (offsetX, offsetY) = (x + offsetX, y + offsetY) 118 | 119 | {------------------------------------------------------------------------------- 120 | Example: Region 121 | ------------------------------------------------------------------------------} 122 | 123 | {- A Region is a set of Positions and is defined by a function that 124 | determines whether a given Position is a member of the set -} 125 | type Region = Position -> Bool 126 | 127 | {- circle returns a circular Region with the given radius, centered at the origin -} 128 | circle :: Distance -> Region 129 | circle radius = fun 130 | where fun position = (magnitude position <= radius) 131 | 132 | {- shift transforms a region by translating it by an offset -} 133 | shift :: Region -> Position -> Region 134 | shift region offset = fun 135 | where fun position = region (translate position (reflect offset)) 136 | 137 | {- invert transforms a region by inverting the set of Positions that it contains -} 138 | invert :: Region -> Region 139 | invert region = fun 140 | where fun position = False -- TODO 141 | 142 | {- union constructs a new Region from the union of two Regions -} 143 | -- union :: Region -> Region -> Region 144 | -- TODO 145 | 146 | {- intersection constructs a new Region from the intersection of two Regions -} 147 | -- intersection :: Region -> Region -> Region 148 | -- TODO 149 | 150 | {- difference constructs a new Region containing all the Positions 151 | of the first region that are not members of the second Region -} 152 | -- difference :: Region -> Region -> Region 153 | -- TODO 154 | -------------------------------------------------------------------------------- /examples-solutions.hs: -------------------------------------------------------------------------------- 1 | {------------------------------------------------------------------------------- 2 | Rapid Introduction to Haskell: Variables 3 | ------------------------------------------------------------------------------} 4 | 5 | x :: Integer 6 | x = 3 7 | 8 | pi :: Double 9 | pi = 3.1415926 10 | 11 | b1, b2 :: Bool 12 | b1 = True 13 | b2 = False 14 | 15 | {------------------------------------------------------------------------------- 16 | Rapid Introduction to Haskell: Arithmetic 17 | ------------------------------------------------------------------------------} 18 | 19 | ex01 = 3 + 2 20 | ex02 = 19 - 27 21 | ex03 = 2.35 * 8.6 22 | ex04 = 8.7 / 3.1 23 | ex05 = div 12 5 24 | ex06 = 7 ^ 222 25 | 26 | {------------------------------------------------------------------------------- 27 | Rapid Introduction to Haskell: Boolean Logic 28 | ------------------------------------------------------------------------------} 29 | 30 | ex07 = True && False 31 | ex08 = not (True || False) 32 | ex09 = ('a' == 'a') 33 | ex10 = (16 /= 3) 34 | ex11 = "Haskell" > "C++" 35 | 36 | {------------------------------------------------------------------------------- 37 | Rapid Introduction to Haskell: Functions 38 | ------------------------------------------------------------------------------} 39 | 40 | doubleMe :: Integer -> Integer 41 | doubleMe x = x + x 42 | 43 | quadrupleMe :: Integer -> Integer 44 | quadrupleMe x = doubleMe (doubleMe x) 45 | 46 | hypotenuse :: Double -> Double -> Double 47 | hypotenuse length width = sqrt squaredHypotenuse 48 | where squaredHypotenuse = length^2 + width^2 49 | 50 | hypotenuse2 :: Double -> Double -> Double 51 | hypotenuse2 length width = 52 | let squaredHypotenuse = length^2 + width^2 in 53 | sqrt squaredHypotenuse 54 | 55 | {------------------------------------------------------------------------------- 56 | Rapid Introduction to Haskell: Types 57 | ------------------------------------------------------------------------------} 58 | 59 | {- Position -} 60 | 61 | {- Note: To avoid multiple declarations of Position, 62 | we declare and use the type Position1 below. -} 63 | 64 | type Position1 = (Double, Double) 65 | 66 | position :: Position1 67 | position = (1.25, 2.75) 68 | 69 | {- Note: To avoid multiple declarations of magnitude, 70 | we declare and function magnitude1 below. -} 71 | 72 | magnitude1 :: Position1 -> Double 73 | magnitude1 (x, y) = sqrt (x * x + y * y) 74 | 75 | {- Person -} 76 | 77 | {- A Name is a type synonym for a String. -} 78 | type Name = String 79 | 80 | {- An Age is a type synonym for an Integer. -} 81 | type Age = Integer 82 | 83 | {- A Color is either Red, Green, or Blue. -} 84 | type Color = String 85 | 86 | {- A Person has a name, age, and favorite color. -} 87 | data Person = Person Name Age Color 88 | 89 | glenn :: Person 90 | glenn = Person "Glenn R. Fisher" 22 "Green" 91 | 92 | favoriteColor :: Person -> Color 93 | favoriteColor (Person name age color) = color 94 | 95 | -- favoriteColor glenn == Green 96 | 97 | {------------------------------------------------------------------------------- 98 | Example: Position 99 | ------------------------------------------------------------------------------} 100 | 101 | {- a Distance is a length in space -} 102 | type Distance = Double 103 | 104 | {- a Position is a pair of x and y distances -} 105 | type Position = (Distance, Distance) 106 | 107 | {- reflect returns a new Position reflected over the origin -} 108 | reflect :: Position -> Position 109 | reflect (x, y) = (-x, -y) 110 | 111 | {- magnitude returns a Position's distance from the origin -} 112 | magnitude :: Position -> Distance 113 | magnitude (x, y) = sqrt (x * x + y * y) 114 | 115 | {- translate returns a new Position translated by an offset -} 116 | translate :: Position -> Position -> Position 117 | translate (x, y) (offsetX, offsetY) = (x + offsetX, y + offsetY) 118 | 119 | {------------------------------------------------------------------------------- 120 | Example: Region 121 | ------------------------------------------------------------------------------} 122 | 123 | {- A Region is a set of Positions and is defined by a function that 124 | determines whether a given Position is a member of the set -} 125 | type Region = Position -> Bool 126 | 127 | {- circle returns a circular Region with the given radius, centered at the origin -} 128 | circle :: Distance -> Region 129 | circle radius = fun 130 | where fun position = (magnitude position <= radius) 131 | 132 | {- shift transforms a region by translating it by an offset -} 133 | shift :: Region -> Position -> Region 134 | shift region offset = fun 135 | where fun position = region (translate position (reflect offset)) 136 | 137 | {- invert transforms a region by inverting the set of Positions that it contains -} 138 | invert :: Region -> Region 139 | invert region = fun 140 | where fun position = not (region position) 141 | 142 | {- union constructs a new Region from the union of two Regions -} 143 | union :: Region -> Region -> Region 144 | union region1 region2 = fun 145 | where fun position = (region1 position) || (region2 position) 146 | 147 | {- intersection constructs a new Region from the intersection of two Regions -} 148 | intersection :: Region -> Region -> Region 149 | intersection region1 region2 = fun 150 | where fun position = (region1 position) && (region2 position) 151 | 152 | {- difference constructs a new Region containing all the Positions 153 | of the first region that are not members of the second Region -} 154 | difference :: Region -> Region -> Region 155 | difference region minus = 156 | intersection region (invert minus) 157 | -------------------------------------------------------------------------------- /figures/MIL_Logo_BW.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/MIL_Logo_BW.png -------------------------------------------------------------------------------- /figures/diffRegion1.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/diffRegion1.ai -------------------------------------------------------------------------------- /figures/diffRegion1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/diffRegion1.png -------------------------------------------------------------------------------- /figures/diffRegion2.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/diffRegion2.ai -------------------------------------------------------------------------------- /figures/diffRegion2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/diffRegion2.png -------------------------------------------------------------------------------- /figures/intersectionRegion1.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/intersectionRegion1.ai -------------------------------------------------------------------------------- /figures/intersectionRegion1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/intersectionRegion1.png -------------------------------------------------------------------------------- /figures/intersectionRegion2.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/intersectionRegion2.ai -------------------------------------------------------------------------------- /figures/intersectionRegion2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/intersectionRegion2.png -------------------------------------------------------------------------------- /figures/invertedRegion.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/invertedRegion.ai -------------------------------------------------------------------------------- /figures/invertedRegion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/invertedRegion.png -------------------------------------------------------------------------------- /figures/region.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/region.ai -------------------------------------------------------------------------------- /figures/region.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/region.png -------------------------------------------------------------------------------- /figures/shiftedRegion.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/shiftedRegion.ai -------------------------------------------------------------------------------- /figures/shiftedRegion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/shiftedRegion.png -------------------------------------------------------------------------------- /figures/unionRegion1.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/unionRegion1.ai -------------------------------------------------------------------------------- /figures/unionRegion1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/unionRegion1.png -------------------------------------------------------------------------------- /figures/unionRegion2.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/unionRegion2.ai -------------------------------------------------------------------------------- /figures/unionRegion2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/figures/unionRegion2.png -------------------------------------------------------------------------------- /presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glennrfisher/introduction-to-functional-programming/c948c6452cc433c401294c796ad98cdde8103d0f/presentation.pdf -------------------------------------------------------------------------------- /presentation.tex: -------------------------------------------------------------------------------- 1 | \documentclass[8pt,aspectratio=169]{beamer} 2 | \usepackage{minted} 3 | 4 | % There are many different themes available for Beamer. A comprehensive 5 | % list with examples is given here: 6 | % http://deic.uab.es/~iblanes/beamer_gallery/index_by_theme.html 7 | % You can uncomment the themes below if you would like to use a different 8 | % one: 9 | %\usetheme{AnnArbor} 10 | %\usetheme{Antibes} 11 | %\usetheme{Bergen} 12 | %\usetheme{Berkeley} 13 | %\usetheme{Berlin} 14 | %\usetheme{Boadilla} 15 | %\usetheme{boxes} 16 | %\usetheme{CambridgeUS} 17 | %\usetheme{Copenhagen} 18 | %\usetheme{Darmstadt} 19 | %\usetheme{default} 20 | %\usetheme{Frankfurt} 21 | %\usetheme{Goettingen} 22 | %\usetheme{Hannover} 23 | %\usetheme{Ilmenau} 24 | %\usetheme{JuanLesPins} 25 | %\usetheme{Luebeck} 26 | \usetheme{Madrid} 27 | %\usetheme{Malmoe} 28 | %\usetheme{Marburg} 29 | %\usetheme{Montpellier} 30 | %\usetheme{PaloAlto} 31 | %\usetheme{Pittsburgh} 32 | %\usetheme{Rochester} 33 | %\usetheme{Singapore} 34 | %\usetheme{Szeged} 35 | %\usetheme{Warsaw} 36 | 37 | \title{An Introduction to Functional Programming} 38 | 39 | \author{Glenn R. Fisher} 40 | % - Give the names in the same order as the appear in the paper. 41 | % - Use the \inst{?} command only if the authors have different 42 | % affiliation. 43 | 44 | \institute[IBM Mobile Innovation Lab] % (optional, but mostly needed) 45 | 46 | % \date{Conference Name, 2013} 47 | % - Either use conference name or its abbreviation. 48 | % - Not really informative to the audience, more for people (including 49 | % yourself) who are reading the slides online 50 | 51 | \subject{Programming Languages} 52 | % This is only inserted into the PDF information catalog. Can be left 53 | % out. 54 | 55 | % If you have a file called "organization-logo-filename.xxx", where xxx 56 | % is a graphic format that can be processed by latex or pdflatex, 57 | % resp., then you can add a logo as follows: 58 | 59 | \pgfdeclareimage[height=0.5cm]{organization-logo}{figures/MIL_Logo_BW} 60 | \logo{\pgfuseimage{organization-logo}} 61 | 62 | % Delete this, if you do not want the table of contents to pop up at 63 | % the beginning of each subsection: 64 | \AtBeginSubsection[] 65 | { 66 | \begin{frame}{Outline} 67 | \tableofcontents[currentsection,currentsubsection] 68 | \end{frame} 69 | } 70 | 71 | % Let's get started 72 | \begin{document} 73 | 74 | \begin{frame} 75 | \titlepage 76 | \end{frame} 77 | 78 | \begin{frame}{Outline} 79 | \tableofcontents[pausesections, hideallsubsections] 80 | % You might wish to add the option [pausesections] 81 | \end{frame} 82 | 83 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 84 | % Section - What is Functional Programming? 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 86 | 87 | \section{What is Functional Programming?} 88 | 89 | %%%%%%%%%% 90 | % Slide 91 | %%%%%%%%%% 92 | 93 | \begin{frame}{What is Functional Programming?} 94 | 95 | \pause 96 | Functional programming is a different way to think about writing programs. 97 | \newline 98 | 99 | \begin{itemize} 100 | \item \pause 101 | \textbf{First-Class Functions}: A function can return another function or accept functions as parameters. 102 | \item \pause 103 | \textbf{Lack of State}: There are no assignment statements. Everything is immutable. 104 | \item \pause 105 | \textbf{Expressions (Not Instructions)}: Functions compute results instead of performing actions. 106 | \item \pause 107 | \textbf{Comprehensive Type System}: Create types and catch errors at compile time. 108 | \end{itemize} 109 | 110 | \vspace{5mm} 111 | 112 | \pause 113 | During this talk, we'll try to develop \textbf{intuition} behind these ideas. 114 | 115 | \end{frame} 116 | 117 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 118 | % Section - Why Learn Functional Programming? 119 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 120 | 121 | \section{Why Learn Functional Programming?} 122 | 123 | %%%%%%%%%% 124 | % Slide 125 | %%%%%%%%%% 126 | 127 | \begin{frame}{Why Learn Functional Programming?} 128 | 129 | \pause 130 | What are the benefits of thinking and programming functionally? 131 | \newline 132 | 133 | \begin{itemize} 134 | \item \pause 135 | Programs are easier to \textbf{understand}. 136 | \item \pause 137 | Shorter, cleaner, and more \textbf{maintainable} code. 138 | \item \pause 139 | Fewer errors. Higher \textbf{reliability}. 140 | \item \pause 141 | Excellent \textbf{performance} with easy parallelism and concurrency. 142 | \end{itemize} 143 | 144 | \end{frame} 145 | 146 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 147 | % Section - Where is Functional Programming Used? 148 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 149 | 150 | \section{Where is Functional Programming Used?} 151 | 152 | %%%%%%%%%% 153 | % Slide 154 | %%%%%%%%%% 155 | 156 | \begin{frame}{Where is Functional Programming Used?} 157 | 158 | \pause 159 | Companies Using Functional Languages: 160 | 161 | \begin{columns}[onlytextwidth] 162 | \begin{column}{0.5\textwidth} 163 | \begin{center} 164 | \begin{itemize} 165 | \item Twitter 166 | \item Jane Street Capital 167 | \item Airbnb 168 | \item Intel 169 | \item LinkedIn 170 | \end{itemize} 171 | \end{center} 172 | \end{column} 173 | \begin{column}{0.5\textwidth} 174 | \begin{center} 175 | \begin{itemize} 176 | \item Foursquare 177 | \item AT\&T 178 | \item Bank of America 179 | \item NVIDIA 180 | \item And Many More... 181 | \end{itemize} 182 | \end{center} 183 | \end{column} 184 | \end{columns} 185 | 186 | \vspace{10mm} 187 | 188 | \pause 189 | You don't need to use a functional programming language to think and program functionally. 190 | \newline 191 | 192 | \pause 193 | \hspace*{20pt} 194 | Functional Programming != Functional Programming Languages 195 | 196 | \pause 197 | \hspace*{20pt} 198 | (Although functional programming languages force you to think functionally.) 199 | \newline 200 | 201 | \end{frame} 202 | 203 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 204 | % Section - Rapid Introduction to Haskell 205 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 206 | 207 | \section{Rapid Introduction to Haskell} 208 | 209 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 210 | % Section - Rapid Introduction to Haskell 211 | % Subsection - Variables 212 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 213 | 214 | \subsection{Variables} 215 | 216 | %%%%%%%%%% 217 | % Slide 218 | %%%%%%%%%% 219 | 220 | \begin{frame}[fragile]{Rapid Introduction to Haskell: Variables} 221 | 222 | \pause 223 | \begin{minted}{haskell} 224 | x :: Integer 225 | x = 3 226 | \end{minted} 227 | 228 | \pause 229 | \begin{minted}{haskell} 230 | pi :: Double 231 | pi = 3.1415926 232 | \end{minted} 233 | 234 | \pause 235 | \begin{minted}{haskell} 236 | b1, b2 :: Bool 237 | b1 = True 238 | b2 = False 239 | \end{minted} 240 | 241 | \end{frame} 242 | 243 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 244 | % Section - Rapid Introduction to Haskell 245 | % Subsection - Arithmetic 246 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 247 | 248 | \subsection{Arithmetic} 249 | 250 | %%%%%%%%%% 251 | % Slide 252 | %%%%%%%%%% 253 | 254 | \begin{frame}[fragile]{Rapid Introduction to Haskell: Arithmetic} 255 | 256 | \pause 257 | \begin{minted}{haskell} 258 | ex01 = 3 + 2 259 | \end{minted} 260 | 261 | \pause 262 | \begin{minted}{haskell} 263 | ex02 = 19 - 27 264 | \end{minted} 265 | 266 | \pause 267 | \begin{minted}{haskell} 268 | ex03 = 2.35 * 8.6 269 | \end{minted} 270 | 271 | \pause 272 | \begin{minted}{haskell} 273 | ex04 = 8.7 / 3.1 274 | \end{minted} 275 | 276 | \pause 277 | \begin{minted}{haskell} 278 | ex05 = div 12 5 279 | \end{minted} 280 | 281 | \pause 282 | \begin{minted}{haskell} 283 | ex06 = 7 ^ 222 284 | \end{minted} 285 | 286 | \end{frame} 287 | 288 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 289 | % Section - Rapid Introduction to Haskell 290 | % Subsection - A Note on Parentheses 291 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 292 | 293 | \subsection{A Note on Parentheses} 294 | 295 | %%%%%%%%%% 296 | % Slide 297 | %%%%%%%%%% 298 | 299 | \begin{frame}[fragile]{Rapid Introduction to Haskell: A Note on Parentheses} 300 | 301 | \pause 302 | Many languages use parentheses to call functions. 303 | 304 | \pause 305 | \begin{minted}{java} 306 | sum(3, 4, 5); 307 | \end{minted} 308 | 309 | \vspace{5mm} 310 | 311 | \pause 312 | In Haskell, we omit the parentheses and use spaces instead. 313 | 314 | \pause 315 | \begin{minted}{haskell} 316 | sum 3 4 5 317 | \end{minted} 318 | 319 | \vspace{5mm} 320 | 321 | \pause 322 | However, we may need parentheses to compute an argument before calling the function. 323 | 324 | \pause 325 | \begin{minted}{haskell} 326 | sum (1 + 2) 4 5 327 | \end{minted} 328 | 329 | \end{frame} 330 | 331 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 332 | % Section - Rapid Introduction to Haskell 333 | % Subsection - Boolean Logic 334 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 335 | 336 | \subsection{Boolean Logic} 337 | 338 | %%%%%%%%%% 339 | % Slide 340 | %%%%%%%%%% 341 | 342 | \begin{frame}[fragile]{Rapid Introduction to Haskell: Boolean Logic} 343 | 344 | \pause 345 | \begin{minted}{haskell} 346 | ex07 = True && False 347 | \end{minted} 348 | 349 | \pause 350 | \begin{minted}{haskell} 351 | ex08 = not (True || False) 352 | \end{minted} 353 | 354 | \pause 355 | \begin{minted}{haskell} 356 | ex09 = ('a' == 'a') 357 | \end{minted} 358 | 359 | \pause 360 | \begin{minted}{haskell} 361 | ex10 = (16 /= 3) 362 | \end{minted} 363 | 364 | \pause 365 | \begin{minted}{haskell} 366 | ex11 = "Haskell" > "C++" 367 | \end{minted} 368 | 369 | \end{frame} 370 | 371 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 372 | % Section - Rapid Introduction to Haskell 373 | % Subsection - Functions 374 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 375 | 376 | \subsection{Functions} 377 | 378 | %%%%%%%%%% 379 | % Slide 380 | %%%%%%%%%% 381 | 382 | \begin{frame}[fragile]{Rapid Introduction to Haskell: Functions} 383 | 384 | \pause 385 | \begin{minted}{haskell} 386 | doubleMe :: Integer -> Integer 387 | doubleMe x = x + x 388 | \end{minted} 389 | 390 | \pause 391 | \begin{minted}{haskell} 392 | quadrupleMe :: Integer -> Integer 393 | quadrupleMe x = doubleMe (doubleMe x) 394 | \end{minted} 395 | 396 | \end{frame} 397 | 398 | %%%%%%%%%% 399 | % Slide 400 | %%%%%%%%%% 401 | 402 | \begin{frame}[fragile]{Rapid Introduction to Haskell: Functions} 403 | 404 | We can also write functions with local variables. 405 | 406 | \pause 407 | \begin{minted}{haskell} 408 | hypotenuse :: Double -> Double -> Double 409 | hypotenuse length width = sqrt squaredHypotenuse 410 | where squaredHypotenuse = length^2 + width^2 411 | \end{minted} 412 | 413 | \pause 414 | \begin{minted}{haskell} 415 | hypotenuse2 :: Double -> Double -> Double 416 | hypotenuse2 length width = 417 | let squaredHypotenuse = length^2 + width^2 in 418 | sqrt squaredHypotenuse 419 | \end{minted} 420 | 421 | \end{frame} 422 | 423 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 424 | % Section - Rapid Introduction to Haskell 425 | % Subsection - Types 426 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 427 | 428 | \subsection{Types} 429 | 430 | %%%%%%%%%% 431 | % Slide 432 | %%%%%%%%%% 433 | 434 | \begin{frame}[fragile]{Rapid Introduction to Haskell: Types} 435 | 436 | \pause 437 | \begin{minted}{haskell} 438 | type Position = (Double, Double) 439 | \end{minted} 440 | 441 | \pause 442 | \begin{minted}{haskell} 443 | position :: Position 444 | position = (1.25, 2.75) 445 | \end{minted} 446 | 447 | \pause 448 | \begin{minted}{haskell} 449 | magnitude :: Position -> Double 450 | magnitude (x, y) = sqrt (x * x + y * y) 451 | \end{minted} 452 | 453 | \vspace{5mm} 454 | 455 | \pause 456 | (We will return to this example later.) 457 | 458 | \end{frame} 459 | 460 | %%%%%%%%%% 461 | % Slide 462 | %%%%%%%%%% 463 | 464 | \begin{frame}[fragile]{Rapid Introduction to Haskell: Types} 465 | 466 | \pause 467 | A Person has a name, age, and favorite color. 468 | 469 | \pause 470 | \begin{minted}{haskell} 471 | type Name = String 472 | \end{minted} 473 | 474 | \pause 475 | \begin{minted}{haskell} 476 | type Age = Integer 477 | \end{minted} 478 | 479 | \pause 480 | \begin{minted}{haskell} 481 | type Color = String 482 | \end{minted} 483 | 484 | \pause 485 | \begin{minted}{haskell} 486 | data Person = Person Name Age Color 487 | \end{minted} 488 | 489 | \pause 490 | \begin{minted}{haskell} 491 | glenn :: Person 492 | glenn = Person "Glenn R. Fisher" 22 "Green" 493 | \end{minted} 494 | 495 | \pause 496 | \begin{minted}{haskell} 497 | favoriteColor :: Person -> Color 498 | favoriteColor (Person name age color) = color 499 | \end{minted} 500 | 501 | \pause 502 | \begin{minted}{haskell} 503 | favoriteColor glenn == "Green" 504 | \end{minted} 505 | 506 | \end{frame} 507 | 508 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 509 | % Section - Example: Position 510 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 511 | 512 | \section{Example: Position} 513 | 514 | %%%%%%%%%% 515 | % Slide 516 | %%%%%%%%%% 517 | 518 | \begin{frame}[fragile]{Position} 519 | 520 | Let's create a Position type and write associated functions. 521 | \newline 522 | 523 | \pause 524 | \begin{minted}{haskell} 525 | {- a Distance is a length in space -} 526 | type Distance = Double 527 | \end{minted} 528 | 529 | \pause 530 | \begin{minted}{haskell} 531 | {- a Position is a pair of x and y distances -} 532 | type Position = (Distance, Distance) 533 | \end{minted} 534 | 535 | \pause 536 | \begin{minted}{haskell} 537 | {- reflect returns a new Position reflected over the origin -} 538 | reflect :: Position -> Position 539 | reflect (x, y) = (-x, -y) 540 | \end{minted} 541 | 542 | \pause 543 | \begin{minted}{haskell} 544 | {- magnitude returns a Position's distance from the origin -} 545 | magnitude :: Position -> Distance 546 | magnitude (x, y) = sqrt (x * x + y * y) 547 | \end{minted} 548 | 549 | \pause 550 | \begin{minted}{haskell} 551 | {- translate returns a new Position translated by an offset -} 552 | translate :: Position -> Position -> Position 553 | translate (x, y) (offsetX, offsetY) = (x + offsetX, y + offsetY) 554 | \end{minted} 555 | 556 | \end{frame} 557 | 558 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 559 | % Section - Example: Region 560 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 561 | 562 | \section{Example: Region} 563 | 564 | %%%%%%%%%% 565 | % Slide 566 | %%%%%%%%%% 567 | 568 | \begin{frame}[fragile]{Example: Region} 569 | 570 | Let's create a Region type and write associated functions. 571 | 572 | \pause 573 | How should we represent a region in 2D space? 574 | 575 | \begin{columns}[onlytextwidth] 576 | \begin{column}{0.45\textwidth} 577 | 578 | \pause 579 | \begin{center} 580 | \includegraphics[scale=0.3]{figures/region} 581 | \end{center} 582 | 583 | \end{column} 584 | \begin{column}{0.55\textwidth} 585 | 586 | \pause 587 | \begin{minted}{haskell} 588 | type Region = Position -> Bool 589 | \end{minted} 590 | 591 | \pause 592 | \begin{minted}{haskell} 593 | circle3 (2, 2) == True 594 | \end{minted} 595 | 596 | \pause 597 | \begin{minted}{haskell} 598 | circle3 (2, 3) == False 599 | \end{minted} 600 | 601 | \pause 602 | \begin{minted}{haskell} 603 | circle3 :: Region 604 | circle3 position = (magnitude position <= 3.0) 605 | \end{minted} 606 | 607 | \pause 608 | \begin{minted}{haskell} 609 | circle4 :: Region 610 | circle4 position = (magnitude position <= 4.0) 611 | \end{minted} 612 | 613 | \pause 614 | \begin{minted}{haskell} 615 | circle :: Distance -> Region 616 | circle radius = fun 617 | where fun position = (magnitude position <= radius) 618 | \end{minted} 619 | 620 | \pause 621 | \begin{minted}{haskell} 622 | easyCircle3 = circle 3.0 623 | easyCircle4 = circle 4.0 624 | \end{minted} 625 | 626 | \end{column} 627 | \end{columns} 628 | 629 | \end{frame} 630 | 631 | %%%%%%%%%% 632 | % Slide 633 | %%%%%%%%%% 634 | 635 | \begin{frame}[fragile]{Example: Region} 636 | 637 | What if we don't want our circle to be centered at the origin? 638 | 639 | \begin{columns}[onlytextwidth] 640 | \begin{column}{0.5\textwidth} 641 | \pause 642 | \begin{center} 643 | \includegraphics[scale=0.22]{figures/region} 644 | \end{center} 645 | \end{column} 646 | \begin{column}{0.5\textwidth} 647 | \pause 648 | \begin{center} 649 | \includegraphics[scale=0.22]{figures/shiftedRegion} 650 | \end{center} 651 | \end{column} 652 | \end{columns} 653 | 654 | \vspace{3mm} 655 | 656 | % avoid spacing between subsequent lines in block 657 | \setlength\partopsep{-\topsep} 658 | \addtolength\partopsep{-\parskip} 659 | \addtolength\partopsep{0.1cm} 660 | 661 | \pause 662 | \begin{minted}{haskell} 663 | {- shift transforms a region by translating it by an offset -} 664 | shift :: Region -> Position -> Region 665 | \end{minted} 666 | 667 | \pause 668 | \begin{minted}{haskell} 669 | shift region offset = fun 670 | where fun position = region (translate position (reflect offset)) 671 | \end{minted} 672 | 673 | % reset spacing between subsequent lines in block 674 | \setlength\partopsep{2pt} 675 | 676 | \end{frame} 677 | 678 | %%%%%%%%%% 679 | % Slide 680 | %%%%%%%%%% 681 | 682 | \begin{frame}[fragile]{Example: Region} 683 | 684 | What if we want the region outside of the circle? 685 | 686 | \begin{columns}[onlytextwidth] 687 | \begin{column}{0.5\textwidth} 688 | \pause 689 | \begin{center} 690 | \includegraphics[scale=0.22]{figures/region} 691 | \end{center} 692 | \end{column} 693 | \begin{column}{0.5\textwidth} 694 | \pause 695 | \begin{center} 696 | \includegraphics[scale=0.22]{figures/invertedRegion} 697 | \end{center} 698 | \end{column} 699 | \end{columns} 700 | 701 | \vspace{3mm} 702 | 703 | % avoid spacing between subsequent lines in block 704 | \setlength\partopsep{-\topsep} 705 | \addtolength\partopsep{-\parskip} 706 | \addtolength\partopsep{0.1cm} 707 | 708 | \pause 709 | \begin{minted}{haskell} 710 | {- invert transforms a region by inverting the set of Positions that it contains -} 711 | invert :: Region -> Region 712 | \end{minted} 713 | 714 | \pause 715 | \begin{minted}{haskell} 716 | invert region = fun 717 | where fun position = not (region position) 718 | \end{minted} 719 | 720 | % reset spacing between subsequent lines in block 721 | \setlength\partopsep{2pt} 722 | 723 | \end{frame} 724 | 725 | %%%%%%%%%% 726 | % Slide 727 | %%%%%%%%%% 728 | 729 | \begin{frame}[fragile]{Example: Region} 730 | 731 | Can we combine regions to create new regions? 732 | 733 | \begin{columns}[onlytextwidth] 734 | \begin{column}{0.5\textwidth} 735 | \pause 736 | \begin{center} 737 | \includegraphics[scale=0.22]{figures/unionRegion1} 738 | \end{center} 739 | \end{column} 740 | \begin{column}{0.5\textwidth} 741 | \pause 742 | \begin{center} 743 | \includegraphics[scale=0.22]{figures/unionRegion2} 744 | \end{center} 745 | \end{column} 746 | \end{columns} 747 | 748 | \vspace{3mm} 749 | 750 | % avoid spacing between subsequent lines in block 751 | \setlength\partopsep{-\topsep} 752 | \addtolength\partopsep{-\parskip} 753 | \addtolength\partopsep{0.1cm} 754 | 755 | \pause 756 | \begin{minted}{haskell} 757 | {- union constructs a new Region from the union of two Regions -} 758 | union :: Region -> Region -> Region 759 | \end{minted} 760 | 761 | \pause 762 | \begin{minted}{haskell} 763 | union region1 region2 = fun 764 | where fun position = (region1 position) || (region2 position) 765 | \end{minted} 766 | 767 | % reset spacing between subsequent lines in block 768 | \setlength\partopsep{2pt} 769 | 770 | \end{frame} 771 | 772 | %%%%%%%%%% 773 | % Slide 774 | %%%%%%%%%% 775 | 776 | \begin{frame}[fragile]{Example: Region} 777 | 778 | Can we combine regions to create new regions? 779 | 780 | \begin{columns}[onlytextwidth] 781 | \begin{column}{0.5\textwidth} 782 | \pause 783 | \begin{center} 784 | \includegraphics[scale=0.22]{figures/intersectionRegion1} 785 | \end{center} 786 | \end{column} 787 | \begin{column}{0.5\textwidth} 788 | \pause 789 | \begin{center} 790 | \includegraphics[scale=0.22]{figures/intersectionRegion2} 791 | \end{center} 792 | \end{column} 793 | \end{columns} 794 | 795 | \vspace{3mm} 796 | 797 | % avoid spacing between subsequent lines in block 798 | \setlength\partopsep{-\topsep} 799 | \addtolength\partopsep{-\parskip} 800 | \addtolength\partopsep{0.1cm} 801 | 802 | \pause 803 | \begin{minted}{haskell} 804 | {- intersection constructs a new Region from the intersection of two Regions -} 805 | intersection :: Region -> Region -> Region 806 | \end{minted} 807 | 808 | \pause 809 | \begin{minted}{haskell} 810 | intersection region1 region2 = fun 811 | where fun position = (region1 position) && (region2 position) 812 | \end{minted} 813 | 814 | % reset spacing between subsequent lines in block 815 | \setlength\partopsep{2pt} 816 | 817 | \end{frame} 818 | 819 | %%%%%%%%%% 820 | % Slide 821 | %%%%%%%%%% 822 | 823 | \begin{frame}[fragile]{Example: Region} 824 | 825 | Can we combine regions to create new regions? 826 | 827 | \begin{columns}[onlytextwidth] 828 | \begin{column}{0.5\textwidth} 829 | \pause 830 | \begin{center} 831 | \includegraphics[scale=0.22]{figures/diffRegion1} 832 | \end{center} 833 | \end{column} 834 | \begin{column}{0.5\textwidth} 835 | \pause 836 | \begin{center} 837 | \includegraphics[scale=0.22]{figures/diffRegion2} 838 | \end{center} 839 | \end{column} 840 | \end{columns} 841 | 842 | \vspace{3mm} 843 | 844 | % avoid spacing between subsequent lines in block 845 | \setlength\partopsep{-\topsep} 846 | \addtolength\partopsep{-\parskip} 847 | \addtolength\partopsep{0.1cm} 848 | 849 | \pause 850 | \begin{minted}{haskell} 851 | {- difference constructs a new Region containing all the Positions 852 | of the first region that are not members of the second Region -} 853 | difference :: Region -> Region -> Region 854 | \end{minted} 855 | 856 | \pause 857 | \begin{minted}{haskell} 858 | difference region minus = 859 | intersection region (invert minus) 860 | \end{minted} 861 | 862 | % reset spacing between subsequent lines in block 863 | \setlength\partopsep{2pt} 864 | 865 | \end{frame} 866 | 867 | %%%%%%%%%% 868 | % Slide 869 | %%%%%%%%%% 870 | 871 | \begin{frame}[fragile]{Example: Region} 872 | 873 | Let's create a Region type and write associated functions. 874 | \newline 875 | 876 | \pause 877 | \begin{minted}{haskell} 878 | {- A Region is a set of Positions and is defined by a function that 879 | determines whether a given Position is a member of the set -} 880 | type Region = Position -> Bool 881 | \end{minted} 882 | 883 | \pause 884 | \begin{minted}{haskell} 885 | {- circle returns a circular Region with the given radius, centered at the origin -} 886 | circle :: Distance -> Region 887 | circle radius = fun 888 | where fun position = (magnitude position <= radius) 889 | \end{minted} 890 | 891 | \pause 892 | \begin{minted}{haskell} 893 | {- shift transforms a region by translating it by an offset -} 894 | shift :: Region -> Position -> Region 895 | shift region offset = fun 896 | where fun position = region (translate position (reflect offset)) 897 | \end{minted} 898 | 899 | \pause 900 | \begin{minted}{haskell} 901 | {- invert transforms a region by inverting the set of Positions that it contains -} 902 | invert :: Region -> Region 903 | invert region = fun 904 | where fun position = not (region position) 905 | \end{minted} 906 | 907 | \end{frame} 908 | 909 | %%%%%%%%%% 910 | % Slide 911 | %%%%%%%%%% 912 | 913 | \begin{frame}[fragile]{Example: Region} 914 | 915 | \begin{minted}{haskell} 916 | {- intersection constructs a new Region from the intersection of two Regions -} 917 | intersection :: Region -> Region -> Region 918 | intersection region1 region2 = fun 919 | where fun position = (region1 position) && (region2 position) 920 | \end{minted} 921 | 922 | \pause 923 | \begin{minted}{haskell} 924 | {- union constructs a new Region from the union of two Regions -} 925 | union :: Region -> Region -> Region 926 | union region1 region2 = fun 927 | where fun position = (region1 position) || (region2 position) 928 | \end{minted} 929 | 930 | \pause 931 | \begin{minted}{haskell} 932 | {- difference constructs a new Region containing all the Positions 933 | of the first region that are not members of the second Region -} 934 | difference :: Region -> Region -> Region 935 | difference region minus = 936 | intersection region (invert minus) 937 | \end{minted} 938 | 939 | \end{frame} 940 | 941 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 942 | % Section - Summary 943 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 944 | 945 | \section{Summary} 946 | 947 | %%%%%%%%%% 948 | % Slide 949 | %%%%%%%%%% 950 | 951 | \begin{frame}{Summary} 952 | 953 | Functional programming is a different way to think about writing programs. 954 | \newline 955 | 956 | \begin{itemize} 957 | \item 958 | \textbf{First-Class Functions}: A function can return another function or accept functions as parameters. 959 | \item 960 | \textbf{Lack of State}: There are no assignment statements. Everything is immutable. 961 | \item 962 | \textbf{Expressions (Not Instructions)}: Functions compute results instead of performing actions. 963 | \item 964 | \textbf{Comprehensive Type System}: Create types and catch errors at compile time. 965 | \end{itemize} 966 | 967 | \vspace{5mm} 968 | 969 | Hopefully this talk helped you develop \textbf{intuition} behind these ideas. 970 | 971 | \end{frame} 972 | 973 | \end{document} 974 | 975 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 976 | % Tips and tricks from template 977 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 978 | 979 | % % You can reveal the parts of a slide one at a time 980 | % % with the \pause command: 981 | % \begin{frame}{Second Slide Title} 982 | % \begin{itemize} 983 | % \item { 984 | % First item. 985 | % \pause % The slide will pause after showing the first item 986 | % } 987 | % \item { 988 | % Second item. 989 | % } 990 | % % You can also specify when the content should appear 991 | % % by using : 992 | % \item<3-> { 993 | % Third item. 994 | % } 995 | % \item<4-> { 996 | % Fourth item. 997 | % } 998 | % % or you can use the \uncover command to reveal general 999 | % % content (not just \items): 1000 | % \item<5-> { 1001 | % Fifth item. \uncover<6->{Extra text in the fifth item.} 1002 | % } 1003 | % \end{itemize} 1004 | % \end{frame} 1005 | 1006 | % \begin{frame}{Blocks} 1007 | % \begin{block}{Block Title} 1008 | % You can also highlight sections of your presentation in a block, with it's own title 1009 | % \end{block} 1010 | % \begin{theorem} 1011 | % There are separate environments for theorems, examples, definitions and proofs. 1012 | % \end{theorem} 1013 | % \begin{example} 1014 | % Here is an example of an example block. 1015 | % \end{example} 1016 | % \end{frame} 1017 | 1018 | % % Placing a * after \section means it will not show in the 1019 | % % outline or table of contents. 1020 | % \section*{Summary} 1021 | 1022 | % \begin{frame}{Summary} 1023 | % \begin{itemize} 1024 | % \item 1025 | % The \alert{first main message} of your talk in one or two lines. 1026 | % \item 1027 | % The \alert{second main message} of your talk in one or two lines. 1028 | % \item 1029 | % Perhaps a \alert{third message}, but not more than that. 1030 | % \end{itemize} 1031 | 1032 | % \begin{itemize} 1033 | % \item 1034 | % Outlook 1035 | % \begin{itemize} 1036 | % \item 1037 | % Something you haven't solved. 1038 | % \item 1039 | % Something else you haven't solved. 1040 | % \end{itemize} 1041 | % \end{itemize} 1042 | % \end{frame} 1043 | 1044 | % % All of the following is optional and typically not needed. 1045 | % \appendix 1046 | % \section*{\appendixname} 1047 | % \subsection*{For Further Reading} 1048 | 1049 | % \begin{frame}[allowframebreaks] 1050 | % \frametitle{For Further Reading} 1051 | 1052 | % \begin{thebibliography}{10} 1053 | 1054 | % \beamertemplatebookbibitems 1055 | % % Start with overview books. 1056 | 1057 | % \bibitem{Author1990} 1058 | % A.~Author. 1059 | % \newblock {\em Handbook of Everything}. 1060 | % \newblock Some Press, 1990. 1061 | 1062 | 1063 | % \beamertemplatearticlebibitems 1064 | % % Followed by interesting articles. Keep the list short. 1065 | 1066 | % \bibitem{Someone2000} 1067 | % S.~Someone. 1068 | % \newblock On this and that. 1069 | % \newblock {\em Journal of This and That}, 2(1):50--100, 1070 | % 2000. 1071 | % \end{thebibliography} 1072 | % \end{frame} 1073 | --------------------------------------------------------------------------------