Lecture 19: Functions, Types and Data Structures in Haskell

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

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

Shell CSCE 314 TAMU. Haskell Functions

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

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

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

A general introduction to Functional Programming using Haskell

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

Haskell Overview III (3A) Young Won Lim 10/4/16

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

CS 320: Concepts of Programming Languages

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

Programming Languages Fall 2013

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

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

Haskell 98 in short! CPSC 449 Principles of Programming Languages

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

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

CSc 372 Comparative Programming Languages

Programming Language Concepts: Lecture 14

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

CSE 3302 Programming Languages Lecture 8: Functional Programming

Lecture 21: Functional Programming in Python. List Comprehensions

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

CS 440: Programming Languages and Translators, Spring 2019 Mon

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

INTRODUCTION TO FUNCTIONAL PROGRAMMING

CSCE 314 Programming Languages

CS 457/557: Functional Languages

A Gentle Introduction to Haskell 98

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

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

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Exercise 1 ( = 24 points)

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

Programming in Haskell Aug-Nov 2015

CS 11 Haskell track: lecture 1

Introduction to Functional Programming in Haskell 1 / 56

Introduction to Programming, Aug-Dec 2006

Higher Order Functions in Haskell

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

List Functions, and Higher-Order Functions

Haskell An Introduction

Functional Programming in Haskell Part I : Basics

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

Functional Paradigm II

Lecture #23: Conversion and Type Inference

Overloading, Type Classes, and Algebraic Datatypes

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

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

Advanced features of Functional Programming (Haskell)

Logical Methods in... using Haskell Getting Started

Inductive Data Types

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Introduction. chapter Functions

Functional Programming

G Programming Languages - Fall 2012

Exercise 1 ( = 22 points)

SML A F unctional Functional Language Language Lecture 19

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

A Gentle Introduction to Haskell 98 Paul Hudak Yale University Department of Computer Science John Peterson Yale University Department of Computer Sci

Lists. Michael P. Fourman. February 2, 2010

Higher-Order Functions

3. Functional Programming. Oscar Nierstrasz

Parallel Haskell on MultiCores and Clusters

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

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

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

Haskell through HUGS THE BASICS

02157 Functional Programming Tagged values and Higher-order list functions

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

Exercise 1 ( = 18 points)

Imperative languages

Functional Programming I

Haskell 5.1 INTERACTIVE SESSIONS AND THE RUN-TIME SYSTEM

State Monad (3D) Young Won Lim 9/25/17

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

K Reference Card. Complete example

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Advanced Programming Handout 3

Review. Advanced Programming Handout 3. Review. Review. Review. Trees. What are the types of these functions? How about these? f1 x y = [x] : [y]

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Functions. Lecture 6 COP 3014 Spring February 11, 2018

1 Lexical Considerations

Programming with Math and Logic

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

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

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

Thoughts on Assignment 4 Haskell: Flow of Control

Types and Type Inference

An Overview of Miranda

INTRODUCTION TO HASKELL

02157 Functional Programming Lecture 2: Functions, Basic Types and Tuples

Transcription:

The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming Functions are values! Mathematically, a function f associates an element of a set X to a unique element of second set Y We write f::x->y three :: Integer -> > Integer infinity :: Integer square :: Integer -> > Integer smaller :: (Integer, Integer) -> > Integer 2 1

Currying Functions Functional can be applied to a partially resulting in new functions with fewer arguments smaller :: (Integer, Integer) -> > Integer smaller (x,y) = if x <= y then x else y smaller2 :: Integer -> > Integer -> > Integer smaller2 x y = if x <= y then x else y The value of the application smaller2 x is a function with type Integer -> > Integer This is known as currying a function 3 Curried Functions Curried functions help reduce the number of parentheses Parentheses make the syntax of some functional languages (e.g. Lisp, Scheme) ugly Curried functions are useful twice :: (Integer -> > Integer) -> > (Integer -> > Integer) twice f x = f (f x) square :: Integer -> > Integer square x = x * x quad :: Integer -> > Integer quad = twice square Function as argument Function as result 4 2

Operators Operators are functions in infix notation rather than prefix notation E.g. 3 + 4 rather than plus 3 4 Functions can be used in infix notation using the f notation E.g. 3 `plus` 4 Operators can be used in prefix notation using the (op)) notation E.g. (+) 3 4 As any other function, operators can be applied partially using the sections notation E.g. The type of (+3) is Integer -> > Integer 5 Operator Associativity Operators associate from left to right (left( left- associative) ) or from right to left (right( right-associative) E.g. - is left-associative 3 4 5 means (3 4) Operator -> > is right-associative X > > Y > > Z means X -> (Y > (Y > > Z) Exercise: deduce the type of h in h x y = f (g x y) 4) 5 f :: Integer -> > Integer g :: Integer -> > Integer -> > Integer 6 3

Function Definition Functions with no parameters are constants E.g. pi = 3.14 has type pi :: Float The definition of a function can depend on the value of the parameters Definitions with conditional expressions Definitions with guarded equation smaller :: Integer -> > Integer -> > Integer smaller x y = if x <= y then x else y smaller2 x y x <= y = x y > x = y 7 Recursive definitions Functional languages make extensive use of recursion fact :: Integer -> > Integer fact n = if n == 0 then 1 else n * fact (n 1) What is the result of fact -1? The following definition is more a fact n n < 0 = error negative argument for fact n == 0 = 1 otherwise = n * fact (n-1) 8 4

Local Definitions Another useful notation in function definition is a local definition Local declarations are introduced using the keyword where For instance, f :: Integer -> > Integer -> > Integer f x y x <= 10 = x + a x > 10 = x a where a = square b b = y + 1 9 Types The following primitive time are available in Haskell Bool Integer Float Double Char Any expression in Haskell has a type Primitive types Derived types Polymorphic types 10 5

Polymorphic Types Some functions and operations work with many types Polymorphic types are specified using type variables For instance, the curry function has a polymorphic type curry :: ((a, b) -> > c) -> > (a -> > b -> > c) curry f x y = f (x,y) Type variables can be qualified using type classes (*) :: Num a => a -> > a -> > a 11 Lists Lists are the workhorse of functional programming Lists are denoted as sequences of elements separated by commas and enclosed within square brackets E.g. [1,2,3] The previous notation is a shorthand for the List data type E.g. 1:2:3:[] 12 6

Lists Functions Functions that operate on lists often have polymorphic types Polymorphic List length :: [a] -> Integer length [] = 0 length (x:xs) = 1 + length xs Pattern Matching In the previous example, the appropriate definition of the function for the specific arguments was chosen using pattern matching 13 Lists Example Derivation length :: [a] -> Integer length [] = 0 (1) length (x:xs) = 1 + length xs (2) length [1,2,3] = { definition (2) } 1 + length [2,3] = { definition (2) } 1 + 1 + length [3] = { definition of + } 2 + length [3] = { definition (2) } 2 + 1 + length [] = { definition of + } 3 + length [] = { definition (1) } 3 + 0 = { definition of + } 3 14 7

Lists Other Functions head :: [a] -> a head (x:xs) = x tail :: [a] -> [a] tail (x:xs) = xs 15 Data Types New data types can be created using the keyword data Examples data Bool = False True Type Constructor data Color = Red Green Blue Violet Polymorphic Type data Point a = Pt a a Type Constructor 16 8

Polymorphic Types Type constructors have types Pt :: a -> a -> Point data Point a = Pt a a Examples Pt 2.0 3.0 :: Point Float Pt 'a' 'b' :: Point Char Pt True False :: Point Bool 17 Recursive Types Binary Trees data Tree a = Leaf a Branch (Tree a) (Tree a) Branch :: Tree a -> Tree a -> Tree a Leaf :: a -> Tree a Example function that operates on trees fringe :: Tree a -> [a] fringe (Leaf x) = [x] fringe (Branch left right) = fringe left ++ fringe right 18 9

List Comprehensions Lists can be defined by enumeration using list comprehensions Syntax: Generator [ f x x <- xs ] [ (x,y) x <- xs, y <- ys ] Example quicksort [] = [] quicksort (x:xs) = quicksort [y y <- xs, y<x ] ++ [x] ++ quicksort [y y <- xs, y>=x] 19 Reading Assignment A Gentle Introduction to Haskell by Paul Hudak, John Peterson, and Joseph H. Fasel. http://www.haskell.org/tutorial/ Read sections 1 and 2 20 10