Lecture 21: Functional Programming in Python. List Comprehensions

Similar documents
Python: Functions and Generators. Giuseppe Attardi

Lecture 19: Functions, Types and Data Structures in Haskell

Python Lists. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. October 5, 2011

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

Functional Programming - 2. Higher Order Functions

F21SC Industrial Programming: Functional Programming in Python

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

CSci 4223 Principles of Programming Languages

A general introduction to Functional Programming using Haskell

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

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

CS 457/557: Functional Languages

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

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

Higher Order Functions in Haskell

Functional Programming and Haskell

OOP and Scripting in Python Advanced Features

An introduction introduction to functional functional programming programming using usin Haskell

CS 440: Programming Languages and Translators, Spring 2019 Mon

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

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

Lecture 4: Higher Order Functions

Shell CSCE 314 TAMU. Functions continued

Functional Programming Mid-term exam Tuesday 3/10/2017

Higher-Order Functions

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Functional Programming Languages (FPL)

CS 340 Spring 2019 Midterm Exam

1 Modules 2 IO. 3 Lambda Functions. 4 Some tips and tricks. 5 Regex. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 30, / 22

Lecture 10 September 11, 2017

Introduction to Programming: Lecture 6

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

Watch out for the arrows. Recollecting Haskell, Part V. A start on higher types: Mapping, 1. A start on higher types: Mapping, 2.

Introduction to Functional Programming

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

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

Shell CSCE 314 TAMU. Haskell Functions

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

Exercise 1 ( = 24 points)

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

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

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

CSc 372 Comparative Programming Languages

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

LECTURE 3 Python Basics Part 2

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

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

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

Logic - CM0845 Introduction to Haskell

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

Lecture 5: Lazy Evaluation and Infinite Data Structures

CS 320: Concepts of Programming Languages

Questions & Answers 28 Sept.

61A Lecture 3. Friday, September 5

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Haskell An Introduction

CSCI-GA Final Exam

ITERATORS AND STREAMS 9

lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

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

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

CSE 307: Principles of Programming Languages

CSE 307: Principles of Programming Languages

Exercises on ML. Programming Languages. Chanseok Oh

INTRODUCTION TO HASKELL

Box-and-arrow Diagrams

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CSE 3302 Programming Languages Lecture 8: Functional Programming

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Introduction to Python - Part I CNV Lab

CSE 130 [Winter 2014] Programming Languages

Introduction to Functional Programming (Python) John R. Woodward

CS 209 Functional Programming

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

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS558 Programming Languages

Introduction to Functional Programming

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

Functional programming in LISP

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

Haskell Refresher Informatics 2D

CONTROL AND HIGHER ORDER FUNCTIONS 1

Tentamen Functioneel Programmeren 2001 Informatica, Universiteit Utrecht Docent: Wishnu Prasetya

301AA - Advanced Programming

Chapter 15 Functional Programming Languages

HIGHER-ORDER FUNCTIONS

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

Programming Languages. Function-Closure Idioms. Adapted from Dan Grossman's PL class, U. of Washington

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

Programming in Haskell Aug-Nov 2015

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

61A Lecture 6. Friday, September 7

Transcription:

The University of North Carolina at Chapel Hill Spring 2002 Lecture 21: Functional Programming in March 1 1 List Comprehensions Haskell Lists can be defined by enumeration using list comprehensions Syntax: Generator [ f x x <- xs ] [ (x,y) x <- xs, y <- ys ] 2 1

List Comprehensions >>> freshfruit = [' banana', ' loganberry ', 'passion fruit '] >>> [weapon.strip() for weapon in freshfruit] ['banana', 'loganberry', 'passion fruit'] 3 List Comprehensions >>> vec = [2, 4, 6] >>> [3*x for x in vec] [6, 12, 18] >>> [3*x for x in vec if x > 3] [12, 18] >>> [3*x for x in vec if x < 2] [] 4 2

List Comprehensions >>> [{x: x**2} for x in vec] [{2: 4}, {4: 16}, {6: 36}] >>> [[x,x**2] for x in vec] [[2, 4], [4, 16], [6, 36]] >>> [x, x**2 for x in vec] ] # error - parens required for tuples File "<stdin stdin>", line 1, in? [x, x**2 for x in vec] ^ SyntaxError: : invalid syntax 5 List Comprehensions >>> [(x, x**2) for x in vec] [(2, 4), (4, 16), (6, 36)] >>> vec1 = [2, 4, 6] >>> vec2 = [4, 3, -9] >>> [x*y for x in vec1 for y in vec2] [8, 6, -18, 16, 12, -36, 24, 18, -54] >>> [x+y for x in vec1 for y in vec2] [6, 5, -7, 8, 7, -5, 10, 9, -3] >>> [vec1[i]*vec2[i] for i in range(len len(vec1))] [8, 12, -54] 6 3

List Comprehension Quicksort example quicksort [] = [] quicksort (x:xs) = quicksort [y y <- xs, y<x ] ++ [x] ++ quicksort [y y <- xs, y>=x] 7 List Comprehensions def quicksort(list): if (len( len(list) == 0): return [] else: pivot = list[0] l = [] l = l + quicksort([x for x in list[1:] if x < pivot]) l.append(pivot) l = l + quicksort([x for x in list[1:] if x >= pivot]) return l 8 4

Higher-Order Functions Higher-order functions are functions that take other functions as arguments They can be use to implement algorithmic skeletons Generic algorithmic techniques Three predefined higher-order functions are specially useful for working with list map fold filter 9 Map Haskell Applies a function to all the elements of a list map :: (a -> > b) -> > [a] -> > [b] map f [] = [] map f (x : xs) ) = f x : map f xs Examples map square [9, 3] fl [81, 9] map (<3) [1, 5] fl [True, False] 10 5

Map "map(function, sequence)" calls function(item) for each of the sequence's items and returns a list of the return values. For example, to compute some cubes: >>> def cube(x): return x*x*x... >>> map(cube, range(1, 11)) [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] 11 Map More than one sequence may be passed the function must then have as many arguments as there are sequences It is called with the corresponding item from each sequence (or None if some sequence is shorter than another). If None is passed for the function, a function returning its argument(s) is substituted. 12 6

Map Combining these two special cases, we see that "map(none, list1, list2)" is a convenient way of turning a pair of lists into a list of pairs. For example >>> seq = range(8) >>> def square(x): return x*x... >>> map(none, seq,, map(square, seq)) [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)] 13 Zip Zip combines two lists into a list of pairs zip :: [a] -> > [b] -> > [(a,b)] zip [] ys = [] zip (x:xs xs) ) [] = [] zip (x:xs xs) ) (y:ys ys) ) = (x,y):zip(xs xs,ys) 14 7

Filter Haskell Extracts the elements of a list that satisfy a boolean function filter :: (a -> Bool) -> > [a] -> > [a] filter p [] = [] filter p (x : xs) ) = if p x then x : filter p xs else filter p xs Example filter (>3) [1, 5, -5, 10, -10] fl [5, 10] 15 Filter filter(function, sequence)" returns a sequence (of the same type, if possible) consisting of those items from the sequence for which function(item) is true. For example, to compute some primes: >>> def f(x): return x % 2!= 0 and x % 3!= 0... >>> filter(f, range(2, 25)) [5, 7, 11, 13, 17, 19, 23] 16 8

Fold Takes in a function and folds it in between the elements of a list Fold Operator Two flavors: Right-wise fold: [x 1, x 2, x 3 ] fl x 1 (x 2 (x 3 e)) Base Element foldr :: (a -> > b -> > b) -> > b -> > [a] -> > [a] foldr f e [] = [] foldr f e (x:xs xs) ) = f x (foldr( f e xs) 17 Foldl Left-wise fold: [x 1, x 2, x 3 ] fl ((e x 1 ) x 2 ) x 3 foldl :: (a -> > b -> > b) -> > b -> > [a] -> > [a] foldl f e [] = [] foldl f e (x:xs xs) ) = foldl f (f e x) xs Example max a b = if a > b then a else b foldl max 0 [1,2,3] fl 3 18 9

Folding in : Reduce "reduce(func func,, sequence)" returns a single value constructed by calling the binary function func on the first two items of the sequence, then on the result and the next item, and so on. For example, to compute the sum of the numbers 1 through 10: >>> def add(x,y): return x+y... >>> reduce(add, range(1, 11)) 55 If there's only one item in the sequence, its value is returned; if the sequence is empty, an exception is raised. 19 Reduce A third argument can be passed to indicate the starting value. In this case the starting value is returned for an empty sequence, and the function is first applied to the starting value and the first sequence item, then to the result and the next item, and so on. For example, >>> def sum(seq seq):... def add(x,y): return x+y... return reduce(add, seq,, 0)... >>> sum(range(1, 11)) 55 >>> sum([]) 0 20 10

Lambda Abstractions Anonymous functions are also useful They are known as lambda abstractions Haskell map (\x->3*x)( [1,2,3] >>> car = lambda lst: lst[0] >>> cdr = lambda lst: lst[1:] >>> sum2 = lambda lst: : car(lst lst)+car( )+car(cdr(lst)) >>> sum2(range(10)) 1 21 More on Functional Programming Articles by David Mertz http://www- 106.ibm.com/developerworks/linux/library/l- prog.html http://www-106.ibm.com/developerworks/library/l 106.ibm.com/developerworks/library/l- prog2.html 22 11

Reading Assignment tutorial List comprehensions» http://www.python.org/doc/current/tut/node7.html#section007 140000000000000000 List displays» http://www.python.org/doc/current/ref/lists.html#l2h-238 238 Higher-order programming with list» http://www.python.org/doc/current/tut/node7.html#section007 130000000000000000 23 12