An introduction introduction to functional functional programming programming using usin Haskell

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

Lecture 19: Functions, Types and Data Structures in Haskell

A general introduction to Functional Programming using Haskell

Shell CSCE 314 TAMU. Haskell Functions

CS 11 Haskell track: lecture 1

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

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

Shell CSCE 314 TAMU. Functions continued

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

CS 360: Programming Languages Lecture 10: Introduction to Haskell

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

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

CSCE 314 Programming Languages

Haskell through HUGS THE BASICS

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

CS 360: Programming Languages Lecture 12: More Haskell

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

An introduction to functional programming. July 23, 2010

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

CS 440: Programming Languages and Translators, Spring 2019 Mon

Introduction to Functional Programming in Haskell 1 / 56

INTRODUCTION TO FUNCTIONAL PROGRAMMING

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

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

Programming Languages Fall 2013

Introduction. chapter Functions

CS 457/557: Functional Languages

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

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

Functional Programming in Haskell Part I : Basics

Haskell Overview II (2A) Young Won Lim 8/23/16

INTRODUCTION TO HASKELL

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

CSCE 314 Programming Languages

Functional Programming and Haskell

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

Haskell Scripts. Yan Huang

Introduction to Haskell

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

CS 320: Concepts of Programming Languages

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Haskell An Introduction

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

CSE 3302 Programming Languages Lecture 8: Functional Programming

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

CSC324 Principles of Programming Languages

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

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

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Lists. Michael P. Fourman. February 2, 2010

Programming Paradigms

Lecture 2: List algorithms using recursion and list comprehensions

Higher Order Functions in Haskell

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

CSc 372 Comparative Programming Languages

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

CSCE 314 Programming Languages

CSci 4223 Principles of Programming Languages

Topic 7: Algebraic Data Types

G Programming Languages - Fall 2012

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Intro to Haskell Notes: Part 5

Programming with Math and Logic

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

CSCE 314 Programming Languages. Functional Parsers

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Lecture #23: Conversion and Type Inference

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Principles of Programming Languages

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

The Haskell HOP: Higher-order Programming

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Programming Languages 3. Definition and Proof by Induction

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

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

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Lecture 4: Higher Order Functions

Programming Languages

Functional Programming for Logicians - Lecture 1

Background Operators (1E) Young Won Lim 7/7/18

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

First Haskell Exercises

Topic 1: Introduction

CSCC24 Functional Programming Typing, Scope, Exceptions ML

Functional Programming Language Haskell

The type checker will complain that the two branches have different types, one is string and the other is int

Programming Languages Fall Prof. Liang Huang

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

CSE P 501 Exam 11/17/05 Sample Solution

Advanced features of Functional Programming (Haskell)

Transcription:

An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau

Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program is a collection of mathematical functions Purely functional : all variables refer to immutable, persistent values that is, new values can be created, but existing ones cannot be modified! Lazy : expressions ess s are evaluated ed by need 2

Example: QuickSort in Java void qsort(int[] a, int lo, int hi) { if (lo < hi) { int l = lo; int h = hi; int p = a[lo]; do { while (l < h && a[l] <= p) l = l+1; while (h > l && a[h] >= p) h = h-1; if (l < h) { int t = a[l]; a[l] = a[h]; a[h] = t; } } while (l < h); int t = a[h]; a[h] = a[lo]; a[lo] = t; qsort(a, lo, h-1); gq qsort(a, h+1, hi); } } Mini-exercise: Sort the list [4,8,1,7] using QuickSort 3

Example: QuickSort in Haskell qsort [] = [] qsort (x:xs) = qsort lt ++ [x] ++ qsort greq where lt = [y y <- xs, y < x] greq = [y y <- xs, y >= x] The program is the specification! List operations: [] the empty list x:xs adds an element x to the head of a list xs xs ++ ys concatenates two lists xs and ys [x,y,z] abbreviation of x:(y:(z:[])) 4

Overview Definitions, functions, expressions, and values Strong typing Recursive data structures Higher-order functions Programming styles, performance Reasoning about program behavior 5

Definitions, functions, expressions, and values len [] = 0 len (x:xs) = len xs + 1 numbers = [7,9,13] n = len numbers Function application can be written without parentheses Functions are reminiscent of methods in Java, but 6

Evaluation through pattern matching and expression rewriting i len [] = 0 len (x:xs) = len xs + 1 numbers = [7,9,13] n = len numbers n len numbers len [7,9,13] len 7:[9,13] len [9,13] + 1 len 9:[13] + 1 len [13] + 1 + 1 len 13:[] + 1 + 1 len [] + 1 + 1 + 1 0 + 1 + 1 + 1 1 + 1 + 1 2 + 1 3 Actual evaluation may proceed in a different order, but the resulting value is the same 7

let expressions len [] = 0 len (x:xs) = len xs + 1 numbers = [7,9,13] n = len numbers numbers = [7,9,13] n = let len [] = 0 len (x:xs) = len xs + 1 in len numbers 8

where clauses len [] = 0 len (x:xs) = len xs + 1 numbers = [7,9,13] n = len numbers numbers = [7,9,13] n = let len [] = 0 len (x:xs) = len xs + 1 in len numbers numbers = [7,9,13], n = len numbers where len [] = 0 len (x:xs) = len xs + 1 9

case expressions len [] = 0 len (x:xs) = len xs + 1 numbers = [7,9,13] n = len numbers numbers = [7,9,13] n = let len [] = 0 len ys = case len ys (x:xs) of = len xs + 1 in [] len -> numbers 0 numbers = [7,9,13] x:xs -> len xs + 1 n = len numbers where numbers len = [][7,9,13] = 0 n = len (x:xs) numbers = len xs + 1 10

Wildcard pattern len [] = 0 len (x:xs) = len xs + 1 numbers = [7,9,13] n = len numbers len (x:xs) = len xs + 1 numbers len _ = = [7,9,13] 0 n = let len [] = 0 len ys = numbers case len ys (x:xs) = of [7,9,13] = len xs + 1 in n = [] len -> numbers 0 numbers = [7,9,13] x:xs -> len xs + 1 n = len numbers where numbers len = [][7,9,13] = 0 n = len (x:xs) numbers = len xs + 1 11

Pattern matching, if-then-else, else, and guards Multiple function definitions with the same function name case expressions if-then-else expressions: if e 1 then e 2 else e case e 1 of 3 True -> e 2 Guards: False -> e 3 sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 12

Iteration and recursion We don t need loops when we have recursion Divide and conquer! len [] = 0 len (x:xs) = len xs + 1 qsort [] = [] qsort (x:xs) = qsort lt ++ [x] ++ qsort greq where lt = [y y <- xs, y < x] greq = [y y <- xs, y >= x] 13

Layout of Haskell programs numbers = [7,9,13] n = let len [] = 0 len (x:xs) = len xs + 1 in len numbers len (x:xs) = len xs + 1 in len numbers numbers = [7,9,13] n = let len [] = 0 numbers = [7,9,13] n = let {len [] = 0; len (x:xs) = len xs + 1} in len numbers 14

Comments -- computes the length of a list len [] = 0 len (x:xs) = len xs + 1 {- numbers = [7,9,13] n = len numbers -} 15

Quiz! Write a function rev that reverses a given list for example, rev [7,9,13] [13,9,7] (don t worry about performance for now...) Hint: recall the definition of len: len [] = 0 len (x:xs) = len xs + 1 16

Overview Definitions, functions, expressions, and values Strong typing Recursive data structures Higher-order functions Programming styles, performance Reasoning about program behavior 17

Strong typing len :: [Integer] -> Integer len [] = 0 len (x:xs) = len xs + 1 numbers :: [Integer] numbers = [7,9,13] n :: Integer n = len numbers Every expression has a type The Haskell static type checker makes sure you are not mixing i apples and bananas 18

Types Integer, String, Float, Char,... base types [X] a list of X values X -> Y a function from X values to Y values (X,Y,Z) a tuple of an X value, a Y value, and a Z value... pair_sum :: (Integer,Integer) -> Integer pair_sum (a,b) = a + b x :: (Integer,(String,Integer),Char) x = (7,( Foo,42), a ) 19

Type errors at compile-time qsort [] = [] qsort (x:xs) = qsort lt + [x] + qsort greq where lt = [y y <- xs, y < x] Can you spot the error? The Hugs tool: greq = [y y <- xs, y >= x] ERROR: example.hs :42 Cannot infer instance *** Instance : Num [a] *** Expression : qsort The error is found, but the quality of type error messages is not always great... 20

Type inference A type annotation is a contract between the author and the user of a function definition In Haskell, writing type annotations is optional use them to express the intended meaning of your functions omit them if the meaning is obvious (Compare this with Java!) When omitted, Haskell finds the type for you!...in fact, the best possible type! (also called, the principal type) More about type inference later... 21

Overview Definitions, functions, expressions, and values Strong typing Recursive data structures Higher-order functions Programming styles, performance Reasoning about program behavior 22

Type definitions data Color = Red Green Blue f Red = 1 f Green = 2 f Blue data = Shape 3 = Rectangle Float Float Circle Float area (Rectangle x y) = x * y area (Circle r) = pi * r * r Red, Green, Blue, Rectangle, and Circle are called constructors t 23

Constructors vs. pattern matching Constructors are special functions that construct values example: Rectangle 3.0 4.0 constructs a Shape value Pattern matching can be used to destruct values example: getx (Rectangle x y) = x defines a function that can extract the x component of a Rectangle value 24

Recursive data structures Type definitions can be recursive! data Expr = Const Float Add Expr Expr Neg Expr Mult Expr Expr eval (Const c) = c Mult Const Add 2.0 eval (Add e1 e2) = eval e1 + eval e2 eval (Neg e) = - eval e eval (Mult e1 e2) = eval e1 * eval e2 Const Const 3.0 4.0 eval (Mult (Const 2.0) (Add (Const 3.0) (Const 4.0)))... 14.0 25

Type synonyms type String = [Char] type Name = String data OptionalAddress = None Addr String type Person = (Name,OptionalAddress) Type synonyms are just abbreviations... 26

Naming requirements The naming style we have been using is mandatory: Type names and constructor names begin with an upper-case letter (e.g. Expr or Rectangle) Value names begin with a lower-case letter (e.g. qsort or x) ) 27

Quiz! Given the type definition data TreeNode = Leaf Integer Branch TreeNode TreeNode write a function leafsum that computes the sum of the leaf values for a given TreeNode structure 28

Overview Definitions, functions, expressions, and values Strong typing Recursive data structures Higher-order functions Programming styles, performance Reasoning about program behavior 29

Higher-order functions Functions are first-class values! (unlike methods in Java...) Functions can take functions as arguments and return functions map f [] = [] map f (x:xs) = f x : map f xs numbers = [7,9,13] inc x = x + 1 more_numbers = map inc numbers Function application associates to the left: f x y (f x) y 30

Currying add_a :: (Integer,Integer) Integer) -> Integer add_a (x,y) = x + y add_b :: Integer -> Integer -> Integer add_b x y = x + y inc = add_b 1 add_a takes a pair of integers as argument and returns their sum add_b takes one integer as argument and returns a function that t takes another integer as argument and returns their sum This trick is named after Haskell B. Curry (1900-1982) 31

Anonymous functions A λ-abstractionb t ti is an anonymous function Traditional syntax: λx.exp Haskell syntax: \x -> exp where x is a variable name and exp is an expression that may use x (λ is like a quantifier) inc x = x + 1 inc = \x -> x + 1 add x y = x + y add = \x -> \y -> x + y 32

Infix operators Infix operators (e.g. + or ++) are just binary functions! x + y (+) x y Binary functions can be written with an infix notation add x y x `add` y Operator precedence and associativity can be specified with fixity declarations... 33

Haskell standard prelude A library containing commonly used definitions Examples: [] ++ ys = ys (x:xs) ++ ys = x : (xs ++ ys) True && x = x False && _ = False type String = [Char] data Bool = False True The core of Haskell is quite small Theoretically, everything can be reduced to the λ-calculus... 34

List comprehensions Lists are pervasive in Haskell... lt = [y y <- xs, y < x] means the same as lt = concatmap f xs where f y y < x = [y] otherwise = [] (concatmap is defined in the standard prelude) 35

An operation on lists: zip zip takes two lists as input (Curry style) and returns a list of pairs Example: zip [1,2,3], ['a','b','c'], evaluates to [(1,'a'),(2,'b'),(3,'c')] How is zip defined? zip (x:xs) (y:ys) = (x,y) : zip xs ys zip [] ys = [] zip xs [] = [] 36

Quiz! Explain why the following 3 definitions have the same meaning and describe what they do: (f. g) x = f (g x) (.) f g x = f (g x) f. g = \x -> f (g x) 37

Overview Definitions, functions, expressions, and values Strong typing Recursive data structures Higher-order functions Programming styles, performance Reasoning about program behavior 38

Syntactic redundancy Expression style vs. Declaration style each function is defined d as one expression each function is defined d as a series of equations let where λ arguments on left-hand-side of = case function-level l pattern matching if guards 39

A note on performance Being in math world doesn t mean that we can ignore performance... Assume that we define reverse by reverse [] = [] reverse (x:xs) = reverse xs ++ [x] Recall that ++ is defined by [] ++ ys = ys (x:xs) ++ ys = x : (xs ++ ys) Computing reverse zs takes time O(n 2 ) where n is the length of zs 40

Afasterreverse reverse using an accumulator reverse zs = rev [] zs where rev acc [] = acc rev acc (x:xs) = rev (x:acc) xs reverse [7,9,13] rev [] [7,9,13] rev [7] [9,13] rev [9,7] [13] rev [13,9,7] [] [13,9,7] Computing reverse zs now takes time O(n ) where n is the length of zs Moral: you have to know how programs are evaluated 41

Overview Definitions, functions, expressions, and values Strong typing Recursive data structures Higher-order functions Programming styles, performance Reasoning about program behavior 42

Referential transparency Purely functional means that evaluation has no side-effects a function maps input to output and does nothing else, just like a real mathematical function Referential transparency: equals can be substituted for equals can disregard d order and duplication of evaluation ation 43

Referential transparency Easier for the programmer (and the compiler) to reason about program behavior Example: f(x)*f(x) always has the same behavior as let z=f(x) in z*z (which is not true for Java!) More about proving properties of programs later... 44

Summary Haskell is purely functional Recursive data structures Pattern matching Higher-order ode functions Strong typing advanced features in the next lecture 45