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

Similar documents
An introduction introduction to functional functional programming programming using usin 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

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

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

CS 360: Programming Languages Lecture 12: More Haskell

An introduction to functional programming. July 23, 2010

Haskell through HUGS THE BASICS

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

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

Introduction. chapter Functions

CS 457/557: Functional Languages

Programming Languages Fall 2013

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

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!

Introduction to Haskell

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

Programming Paradigms

Lists. Michael P. Fourman. February 2, 2010

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

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

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";

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

The Haskell HOP: Higher-order Programming

Programming Languages 3. Definition and Proof by Induction

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

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

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

Lecture 4: Higher Order Functions

Programming Languages

Functional Programming for Logicians - Lecture 1

Topic 7: Algebraic Data Types

Programming with Math and Logic

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

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

First Haskell Exercises

CSCC24 Functional Programming Typing, Scope, Exceptions ML

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

Programming Languages Fall Prof. Liang Huang

CSE P 501 Exam 11/17/05 Sample Solution

Functional Programming Language Haskell

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Advanced features of Functional Programming (Haskell)

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

Overloading, Type Classes, and Algebraic Datatypes

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

3. Functional Programming. Oscar Nierstrasz

Transcription:

Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming 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 are evaluated by need 2 Example: QuickSort in Java Example: QuickSort in Haskell 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); qsort(a, h+1, hi); } } Mini-exercise: Sort the list [4,8,1,7] using QuickSort 3 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 1

Overview Definitions, functions, expressions, and values Programming styles, performance Reasoning about program behavior Definitions, functions, expressions, and values Function application can be written without parentheses Functions are reminiscent of methods in Java, but 5 6 Evaluation through pattern matching and expression rewriting 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 let expressions n = let in len numbers Actual evaluation may proceed in a different order, but the resulting value is the same 7 8 2

where clauses case expressions n = let in len numbers where n = let len ys = case len ys (x:xs) of = len xs + 1 in len [] -> numbers 0 x:xs -> len xs + 1 where numbers len [] = [7,9,13] = 0 n = len (x:xs) numbers = len xs + 1 9 10 Wildcard pattern Pattern matching, if-then then-else, else, and guards numbers len _ = = [7,9,13] 0 n = let len ys numbers = case len ys (x:xs) = [7,9,13] of = len xs + 1 n in = [] len -> numbers 0 x:xs -> len xs + 1 where numbers len [] = [7,9,13] = 0 n = len (x:xs) numbers = len xs + 1 Multiple function definitions with the same function name case expressions if-then-else expressions: case e if e 1 then e 2 else e 1 of 3 True -> e 2 False -> e 3 Guards: sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 11 12 3

Iteration and recursion We don t need loops when we have recursion Divide and conquer! qsort [] = [] qsort (x:xs) = qsort lt ++ [x] ++ qsort greq where lt = [y y <- xs, y < x] greq = [y y <- xs, y >= x] Layout of Haskell programs n = let in len numbers n = let in len numbers n = let {; } in len numbers 13 14 Comments Quiz! -- computes the length of a list {- -} 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: 15 16 4

Overview Strong typing Definitions, functions, expressions, and values Programming styles, performance Reasoning about program behavior len :: [Integer] -> Integer numbers :: [Integer] n :: Integer Every expression has a type The Haskell static type checker makes sure you are not mixing apples and bananas 17 18 Types Type errors at compile-time 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... qsort [] = [] qsort (x:xs) = qsort lt + [x] + qsort greq where lt = [y y <- xs, y < x] greq = [y y <- xs, y >= x] Can you spot the error? pair_sum :: (Integer,Integer) Integer) -> Integer pair_sum (a,b) = a + b x :: (Integer,(String,Integer),Char) x = (7,( Foo,42), a ) 19 The Hugs tool: 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 5

Type inference Overview 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) Definitions, functions, expressions, and values Programming styles, performance Reasoning about program behavior More about type inference later... 21 22 Type definitions Constructors vs. pattern matching 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 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 23 24 6

Recursive data structures Type synonyms Type definitions can be recursive! Mult data Expr = Const Float Const Add Add Expr Expr Neg Expr 2.0 Const Const Mult Expr Expr 3.0 4.0 eval (Const c) = c eval (Add e1 e2) = eval e1 + eval e2 eval (Neg e) = - eval e eval (Mult e1 e2) = eval e1 * eval e2 type String = [Char] type Name = String data OptionalAddress = None Addr String type Person = (Name,OptionalAddress) Type synonyms are just abbreviations... eval (Mult (Const 2.0) (Add (Const 3.0) (Const 4.0)))... 14.0 25 26 Naming requirements Quiz! 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) Given the type definition iti data TreeNode = Leaf Integer Branch TreeNode TreeNode write a function leafsum that computes the sum of the leaf values for a given TreeNode structure 27 28 7

Overview Higher-order functions Definitions, functions, expressions, and values Programming styles, performance Reasoning about program behavior 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 inc x = x + 1 more_numbers = map inc numbers Function application associates to the left: f x y (f x) y 29 30 Currying Anonymous functions add_a :: (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 takes another integer as argument and returns their sum A λ-abstraction is an anonymous function Traditional syntax: λx.exp where x is a variable name and exp is an expression that may use x (λ is like a quantifier) Haskell syntax: \x -> exp inc x = x + 1 inc = \x -> x + 1 This trick is named after Haskell B. Curry (1900-1982) 31 add x y = x + y add = \x -> \y -> x + y 32 8

Infix operators Haskell standard prelude Infix operators (e.g. + or ++) are just binary functions! A library containing commonly used definitions x + y (+) x y Examples: [] ++ ys = ys (x:xs) ++ ys = x : (xs ++ ys) Binary functions can be written with an infix notation True && x = x False && _ = False add x y x `add` y type String = [Char] Operator precedence and associativity can be specified with fixity declarations... data Bool = False True The core of Haskell is quite small 33 Theoretically, everything can be reduced to the λ-calculus... 34 List comprehensions An operation on lists: zip Lists are pervasive in Haskell... zip takes two lists as input (Curry style) and returns a list of pairs lt = [y y <- xs, y < x] means the same as lt = concatmap f xs where Example: zip [1,2,3] ['a','b','c'] evaluates to [(1,'a'),(2,'b'),(3,'c')] f y y < x = [y] otherwise = [] (concatmap is defined in the standard prelude) How is zip defined? zip (x:xs) (y:ys) = (x,y) : zip xs ys zip [] ys = [] zip xs [] = [] 35 36 9

Quiz! Explain why the following 3 definitions iti 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) Overview Definitions, functions, expressions, and values Programming styles, performance Reasoning about program behavior 37 38 Syntactic redundancy A note on performance Expression style each function is defined as one expression let vs. Declaration style each function is defined as a series of equations where 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] λ case arguments on left-hand-side of = function-level pattern matching Recall that ++ is defined by [] ++ ys = ys (x:xs) ++ ys = x : (xs ++ ys) if guards 39 Computing reverse zs takes time O(n 2 ) where n is the length of zs 40 10

A faster reverse using an accumulator Overview 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 Definitions, functions, expressions, and values Programming styles, performance Reasoning about program behavior Moral: you have to know how programs are evaluated 41 42 Referential transparency 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 order and duplication of evaluation 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... 43 44 11

Summary Haskell is purely functional Pattern matching advanced features in the next lecture 45 12