An introduction to functional programming. July 23, 2010

Similar documents
Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

Introduction to Haskell

Programming Languages Fall 2013

CS 11 Haskell track: lecture 1

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

An introduction introduction to functional functional programming programming using usin Haskell

Programming Paradigms

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

Haskell An Introduction

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

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

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

Abstract Types, Algebraic Types, and Type Classes

CS 360: Programming Languages Lecture 10: Introduction to Haskell

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

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

Functional Programming in Haskell Part I : Basics

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Advanced features of Functional Programming (Haskell)

A general introduction to Functional Programming using Haskell

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

CSci 450: Org. of Programming Languages Overloading and Type Classes

3. Functional Programming. Oscar Nierstrasz

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Principles of Programming Languages

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

CS 360: Programming Languages Lecture 12: More Haskell

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

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Introduction. chapter Functions

First Haskell Exercises

Haskell through HUGS THE BASICS

Haskell Monads CSC 131. Kim Bruce

Functional Programming and Haskell

Functional Programming

Lecture 19: Functions, Types and Data Structures in Haskell

Functional Programming. Big Picture. Design of Programming Languages

Informatics 1 Functional Programming 19 Tuesday 23 November IO and Monads. Philip Wadler University of Edinburgh

Logic - CM0845 Introduction to Haskell

CSc 372 Comparative Programming Languages

Imperative languages

CSCE 314 Programming Languages

Overloading, Type Classes, and Algebraic Datatypes

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

Introduction to Functional Programming in Haskell 1 / 56

INTRODUCTION TO HASKELL

Programming Paradigms

Programming in Haskell Aug-Nov 2015

CS 320: Concepts of Programming Languages

Advances in Programming Languages

Little and not so big bits of Haskell

Functional Programming for Logicians - Lecture 1

References. Monadic I/O in Haskell. Digression, continued. Digression: Creating stand-alone Haskell Programs

Parallel Haskell on MultiCores and Clusters

Algebraic Types. Chapter 14 of Thompson

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

Functional Programming

Chapter 15. Functional Programming Languages

Simon Peyton Jones Microsoft Research August 2012

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Monad Background (3A) Young Won Lim 11/18/17

Course year Typeclasses and their instances

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

Haskell: From Basic to Advanced. Haskell buzzwords. Hello, World! History. Haskell 98 / Haskell 2010 GHC

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Monad Background (3A) Young Won Lim 11/8/17

D7020E. The dynamic evolution of a system Robust and Energy-Efficient Real-Time Systems. blocked Lecture 2: Components & the Timber language.

D7020E. Robust and Energy-Efficient Real-Time Systems D7020E. Lecture 2: Components & the Timber language

Programming with Math and Logic

Lecture 8: Summary of Haskell course + Type Level Programming

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

CS457/557 Functional Languages

301AA - Advanced Programming [AP-2017]

Functional Programming in Haskell for A level teachers

CSE 3302 Programming Languages Lecture 8: Functional Programming

Functional Programming I *** Functional Programming and Interactive Theorem Proving. Ulrich Berger Michaelmas Term 2006

Functional Logic Programming Language Curry

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

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

CSCE 314 Programming Languages

Introduction to Functional Programming and Haskell. Aden Seaman

Lazy Functional Programming in Haskell

Functional Programming

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

JVM ByteCode Interpreter

CSCI-GA Scripting Languages

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

SML A F unctional Functional Language Language Lecture 19

Informatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh

2 Haskell. Haskell (629) 59. Haskell. B. Curry. 1.2, 1.3, Haskell 98 [16] 1. Haskell. Haskell. ( Haskell.

Transcription:

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