Introduction to Functional Programming and Haskell. Aden Seaman

Similar documents
JVM ByteCode Interpreter

Refactoring to Functional. Hadi Hariri

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CS 11 Haskell track: lecture 1

3. Functional Programming. Oscar Nierstrasz

Functional Programming

LECTURE 16. Functional Programming

Functional Programming in C++

SIMON THOMPSON LEARNING FUNCTIONAL PROGRAMMING

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Programming with Math and Logic

Functional Programming and Haskell

Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

Functional Programming

Stop coding Pascal. Saturday, April 6, 13

Concepts of Programming Languages

Background Type Classes (1B) Young Won Lim 6/28/18

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Imperative languages

INTRODUCTION TO HASKELL

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

Functional Programming in C++

Software System Design and Implementation

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Introduction. chapter Functions

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Box-and-arrow Diagrams

Introduction to Functional Programming in Haskell 1 / 56

Declarative concurrency. March 3, 2014

CMSC 330, Fall 2013, Practice Problems 3

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Functional Programming

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

Haskell in the corporate environment. Jeff Polakow October 17, 2008

Programming Systems in Artificial Intelligence Functional Programming

Functional Programming

CSCE 314 Programming Languages

An introduction to functional programming. July 23, 2010

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CS457/557 Functional Languages

Chapter 15. Functional Programming Languages

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSCI-GA Scripting Languages

Little and not so big bits of Haskell

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Haskell An Introduction

Some Advanced ML Features

CMSC330. Objects, Functional Programming, and lambda calculus

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Haskell Functional Programming

An introduction introduction to functional functional programming programming using usin Haskell

Chapter 15. Functional Programming Languages

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Haskell Monads CSC 131. Kim Bruce

Programming Paradigms and Languages Introduction to Haskell. dr Robert Kowalczyk WMiI UŁ

Programming Paradigms

Introduction to OCaml

Introduction to Haskell

Monday Feb 16, 5pm. solve these equalities. Midterm Thursday Feb 12. covers only ocaml part of the course, assignments 1 through 3.

Lazy Functional Programming in Haskell

Classical Themes of Computer Science

Mostly functional programming does not work

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

CSC324 Principles of Programming Languages

Haskell Functional Programming

Functional Programming Lecture 1: Introduction

What s different about Factor?

Functional Programming. Big Picture. Design of Programming Languages

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Presented By Andrew Butt

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

CMSC330 Spring 2014 Midterm 2 Solutions

Functional Programming Languages (FPL)

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Haskell Refresher Informatics 2D

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Functional Programming

Haskell. COS 326 Andrew W. Appel Princeton University. a lazy, purely functional language. slides copyright David Walker and Andrew W.

Principles of Programming Languages (H)

A general introduction to Functional Programming using Haskell

Is Functional Programming (FP) for me? ACCU Conference 2008 Hubert Matthews

CS252 Advanced Programming Language Principles. Prof. Tom Austin San José State University Fall 2013

Functional Programming Lecture 13: FP in the Real World

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Haskell Overview II (2A) Young Won Lim 9/26/16

Programming Languages Fall 2013

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness

Lecture 4: Higher Order Functions

Functional Programming Principles in Scala. Martin Odersky

CSCE 314 Programming Languages

Do Extraterrestrials Use Functional Programming?

INDEX. Symbols & Numbers. Learn You a Haskell for Great Good! 2011 by Miran Lipovača

Scala : an LLVM-targeted Scala compiler

An Embedded Domain Specific Language to Model, Transform and Quality Assure Business Processes in Business-Driven Development

More on functional programming

Transcription:

Introduction to Functional Programming and Haskell Aden Seaman

Functional Programming

Functional Programming First Class Functions Expressions (No Assignment) (Ideally) No Side Effects Different Approach

Brief History 1930 Lambda Calculus 1958 LISP 1974 ML 1974 Scheme 1986 Erlang 1990 Haskell 1996 OCaml 2004 Scala 2005 F# 2007 Clojure

Who? Academia Language Researchers Growing Interest

Why? Safety Modularity Concurrency Concise Fun Better

Structures Lists [ ] [ 1, 2, 3 ] = 1:2:3:[ ] [ 'a', 'b', 'c' ] = 'a':'b':'c':[ ] Concatenation Indexing Head, Tail head [1, 2, 3] = 1 tail [1, 2, 3] = [2, 3]

Structures Tuples ( 1, 2, hello ) ( world, [ 3, 4, 5 ] ) Bundling Data

Techniques (Only) Recursion fib 1 = 1 fib 2 = 1 fib n = fib (n-1) + fib (n-2)

Techniques Map map f [ a, b, c, d ] [ f a, f b, f c, f d ]

Techniques Filter filter (< 5) [ 1, 5, 9, 3, 4 ] [ 1, 3, 4 ]

Techniques Fold foldl (+) 0 [ 1, 5, 9, 3, 4 ] 22

Language Features First Class Functions f1 x = 2 * x f2 x = 4 * x f3 v f = ( f v ) + 3*( f v ) f3 5 f1 = ( 2*5 ) + 3*( 2*5 ) = 40 f3 5 f2 = ( 4*5 ) + 3*( 4*5 ) = 80

Language Features Pattern Matching Variable Assignment a:b = [ 1, 2, 3, 4, 5 ] a = 1 b = [ 2, 3, 4, 5 ]

Language Features Pattern Matching Variable Assignment ( a, b, c ) = ( 1, hi, [ 2, 3, 4 ] ) a = 1 b = hi c = [ 2, 3, 4 ]

Language Features Pattern Matching Function Definitions f 0 = 1 f 2 = 5 f x = x + 9

Language Features Pattern Matching Combinations f (a,b) = a + b f (1,2) = 3 map f [] = [] map f (h:t) = (f h):(map f t)

Language Features Lambda Functions f (a,b) = a + b f (2,3) = 5 (\(a,b) -> a + b) (2,3) = 5 map (\(a,b) -> a + b) [ (1,2), (3, 4), (5,6) ] [ 3, 7, 11 ]

Language Features Currying f a b = a + b g = f 2 = (2 +) g 3 = (f 2) 3 = (2 +) 3 = 5

Language Features Composition (f. g) x = f (g x) f x = 2*x g x y = 3*x + y h = f. (g 3) h 4 = 2*(g 3 4) = 2*(3*3 + 4) = 26

Language Features Combined Example Lists Tuples Map Filter Fold First Class Functions Pattern Matching Lambda Functions Currying Composition Recursion

Sum numbers under specific headers ==> stuff 1 1 2 3 ==> stuff 2 4 5 6 ==> stuff 3 7 8 9 ==> stuff 2 10 11 12 ==> stuff 1 13 14 15 16 17 18 19 ==> stuff 3 20 ==> stuff 2 21 ==> stuff 1 22 23 24 25

==> stuff 1 1 2 3 ==> stuff 2 4 5 6 ==> stuff 3 7 8 9 ==> stuff 2 10 11 12 ==> stuff 1 13 14 15 16 17 18 19 ==> stuff 3 20 ==> stuff 2 21 ==> stuff 1 22 23 24 25 segmentbyprefix :: String -> [String] -> [[String]] segmentbyprefix prefix lines = tail $ helper [] [] lines where helper totalacc listacc [] = reverse ((reverse listacc):totalacc) helper totalacc listacc (x:xs) isprefixof prefix x = helper ((reverse listacc):totalacc) [x] xs otherwise = helper totalacc (x:listacc) xs

==> stuff 1 1 2 3 ==> stuff 2 4 5 6 ==> stuff 3 7 8 9 ==> stuff 2 10 11 12 ==> stuff 1 13 14 15 16 17 18 19 ==> stuff 3 20 ==> stuff 2 21 ==> stuff 1 22 23 24 25 segmentbyprefix :: String -> [String] -> [[String]] segmentbyprefix prefix lines = tail $ helper [] [] lines where helper totalacc listacc [] = reverse ((reverse listacc):totalacc) helper totalacc listacc (x:xs) isprefixof prefix x = helper ((reverse listacc):totalacc) [x] xs otherwise = helper totalacc (x:listacc) xs

Lists Tuples Map Filter Fold First Class Functions Pattern Matching Lambda Functions Currying Composition Recursion segmentbyprefix :: String -> [String] -> [[String]] segmentbyprefix prefix lines = tail $ helper [] [] lines where helper totalacc listacc [] = reverse ((reverse listacc):totalacc) helper totalacc listacc (x:xs) isprefixof prefix x = helper ((reverse listacc):totalacc) [x] xs otherwise = helper totalacc (x:listacc) xs

==> stuff 1 1 2 3 ==> stuff 2 4 5 6 ==> stuff 3 7 8 9 ==> stuff 2 10 11 12 ==> stuff 1 13 14 15 16 17 18 19 ==> stuff 3 20 ==> stuff 2 21 ==> stuff 1 22 23 24 25 process1 :: String -> String -> String -> [Double] process1 prefix header inputstring = summedsegments where inputlines = lines inputstring segments = segmentbyprefix prefix inputlines headeredsegments = map (\(h:t) -> (h,t)) segments filteredsegments = filter (\(h,_) -> h == header) headeredsegments numbersegments = map (\(h,t) -> map read t :: [Double]) filteredsegments summedsegments = map (foldl (+) 0) numbersegments

Lists Tuples Map Filter Fold First Class Functions Pattern Matching Lambda Functions Currying Composition Recursion process1 :: String -> String -> String -> [Double] process1 prefix header inputstring = summedsegments where inputlines = lines inputstring segments = segmentbyprefix prefix inputlines headeredsegments = map (\(h:t) -> (h,t)) segments filteredsegments = filter (\(h,_) -> h == header) headeredsegments numbersegments = map (\(h,t) -> map read t :: [Double]) filteredsegments summedsegments = map (foldl (+) 0) numbersegments

Lists Tuples Map Filter Fold First Class Functions Pattern Matching Lambda Functions Currying Composition Recursion process2 :: String -> String -> String -> [Double] process2 prefix header = (map (foldl (+) 0)). (map (\(h,t) -> map read t :: [Double])). (filter (\(h,_) -> h == header)). (map (\(h:t) -> (h,t))). (segmentbyprefix prefix). lines

import Data.List (isprefixof) process2 :: String -> String -> String -> [Double] process2 prefix header = (map (foldl (+) 0)). (map (\(h,t) -> map read t :: [Double])). (filter (\(h,_) -> h == header)). (map (\(h:t) -> (h,t))). (segmentbyprefix prefix). lines segmentbyprefix :: String -> [String] -> [[String]] segmentbyprefix prefix lines = tail $ helper [] [] lines where helper totalacc listacc [] = reverse ((reverse listacc):totalacc) helper totalacc listacc (x:xs) isprefixof prefix x = helper ((reverse listacc):totalacc) [x] xs otherwise = helper totalacc (x:listacc) xs main :: IO () main = do inputfilecontents <- readfile "input.file.txt" putstrln $ show (process2 "==>" "==> stuff 1" inputfilecontents) [6.0, 112.0, 94.0]

Language Features Others Immutability Garbage Collection Closures

Closures f x = (\y -> x + y) makeclosure :: ContextData -> (ContextData -> InputData -> OutputData) -> (InputData -> OutputData) makeclosure contextdata processfunction = \inputdata -> processfunction contextdata inputdata makeclosure contextdata processfunction = processfunction contextdata

Closures f x = (\y -> x + y) makeclosure :: ContextData -> (ContextData -> InputData -> OutputData) -> (InputData -> OutputData) makeclosure contextdata processfunction = \inputdata -> processfunction contextdata inputdata makeclosure contextdata processfunction = processfunction contextdata Uninterestingly common in Haskell!

Language Features Absent Loops Multiple Assignment Object Oriented Global Variables

Contemporary Languages Language Pure Typed Lazy OO Haskell OCAML ~ Scala ~ Clojure Erlang

Haskell

Approach Constrained Only the Haskell way Top-Down (Abstractions, Lambda Calc & Category Theory) Safe Fast Powerful

Influences Clean, FP, Gofer, Hope, Id, ISWIM, KRC, Lisp, Miranda, ML, Standard ML, Orwell, SASL, Scheme, SISAL

Influences Clean, FP, Gofer, Hope, Id, ISWIM, KRC, Lisp, Miranda, ML, Standard ML, Orwell, SASL, Scheme, SISAL Unification Language Designed by committee

Language Features Currying Infix Concise Pattern Matching Implicit Functions Lazy Evaluation

Why? Enforced Safety Strongly Typed Single Assignment Pure/Contained IO Smart Shared State Expressions (everything is one)

Why? Separation Fast (reordering, fusion) Parallelism (sparks) Concurrency (Haskell threads) Modularity & Reuse

Why? Lazy Evaluation Infinite Data Structures Efficiency Composition & Modularity Hard to reason about

Why? Others Concise Foreign Function Interface Inline code (Python, Java, C, R) Abstract Power Advanced Features

Purity Safe Referential Transparency Easy to reason about Composable

Type System Strict (hard to get used to) Polymorphism Type Inference Mental Gymnastics Algebraic Data Types Type Classes

Type System Algebraic Data Types data Maybe a = Just a Nothing data Either a b = Left a Right b data List a = Item a (List a) Null data Center = Center { centerx :: Double, centery :: Double } data Shape = Square Center Double Rectangle Center Double Double Circle Center Double

Type System Algebraic Data Types data Maybe a = Just a Nothing data Either a b = Left a Right b data List a = Item a (List a) Null data Center = Center { centerx :: Double, centery :: Double } data Shape = Square Center Double Rectangle Center Double Double Circle Center Double Can be emulated in C and others!!

Type System Types say a lot func1 :: String -> Int func2 :: Num a => a -> a -> a func3 :: Double func4 :: IO Double func5 :: FilePath -> IO String func6 :: String -> IO ()

ADTs + Type Classes Contexts Monoids Functors Applicative Functors Monads etc...

Monads Isolation Containment Context Sensitive Imperative-Like Sugar Haskell in the finest imperative programming language - SPJ

Monads Definition class Applicative m => Monad where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a class Functor f => Applicative where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b class Functor where fmap :: (a -> b) -> f a -> f b

Monads Kinds IO STM ST List, Maybe, Either State, Reader, Writer etc...

Monads Example (Maybe) class Applicative m => Monad where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a instance Monad Maybe where return x = Just x Nothing >>= f = Nothing Just x >>= f = f x fail _ = Nothing (>>) = (*>)

Monads Example (Maybe) checkstring :: String -> Maybe Double checkstring input = do val1 <- readmaybe input :: Double val2 <- inbounds val1 return $ 4.0 + 5.0*val2 inbounds :: Double -> Maybe Double inbounds val val < 10 = Nothing otherwise = Just val checkstring foo = Nothing checkstring 5 = Nothing checkstring 20 = Just 104.0

Side Effects Happen in IO monad Can t escape from IO monad Push side-effects to edges Pure functional core

More Advanced Stuff Applicative Functors Category Theory Concurrency (MVAR, STM) Parallelism Laziness Monad Transformers Pipe/Conduit Arrows Lens Parsers Persistent Free Monads Crazy type extensions Template Haskell (DSLs) Metaprogramming Way more...

FP + Haskell = Cool New Ideas Better Programmer Safer Programmer Amazing Concurrency Very Fast Brain Bending Horizon Expanding Challenging & Fun!