CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

Similar documents
PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

CSc 372 Comparative Programming Languages

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

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

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

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

Shell CSCE 314 TAMU. Haskell Functions

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

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Functional Programming in Haskell Part I : Basics

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

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CSc 372. Comparative Programming Languages. 13 : Haskell Lazy Evaluation. Department of Computer Science University of Arizona

Lecture 19: Functions, Types and Data Structures in Haskell

CSc 372. Comparative Programming Languages. 3 : Haskell Introduction. Department of Computer Science University of Arizona

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CSc 520. Principles of Programming Languages 11: Haskell Basics

Programming Language Concepts: Lecture 14

Assertions & Verification & Example Loop Invariants Example Exam Questions

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

CSc 520 Principles of Programming Languages

Introduction. chapter Functions

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

CSc 520. Principles of Programming Languages 25: Types Introduction

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

Assertions & Verification Example Exam Questions

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

G Programming Languages - Fall 2012

60-265: Winter ANSWERS Exercise 4 Combinational Circuit Design

520 Principles of Programming Languages. Arithmetic. Variable Declarations. 19: Pascal

CS 320: Concepts of Programming Languages

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Imperative Paradigm. Syntax. Role of Programming Languages. Expressions. Expressions. Role of Programming Languages. Symbols.

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

CSc 372 Comparative Programming Languages

Patterns The Essence of Functional Programming

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

A general introduction to Functional Programming using Haskell

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

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

Declaring Numbers. Bernd Braßel, Frank Huch and Sebastian Fischer. Department of Computer Science, University of Kiel, Germany

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

Lecture 2: List algorithms using recursion and list comprehensions

Haskell through HUGS THE BASICS

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Exercise 1 ( = 22 points)

Introductory Example

CS 320: Concepts of Programming Languages

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Sorting. Bubble Sort. Selection Sort

CSc 372. Comparative Programming Languages. 18 : Haskell Type Classes. Department of Computer Science University of Arizona

Data Dependence Analysis

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

Functional Programming Languages (FPL)

CSE399: Advanced Programming. Handout 2

Loops / Repetition Statements

Flow Control. CSC215 Lecture

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

CSc 520. Gofer III. Accumulative Recursion. Accumulative Recursion... Stack Recursion. Principles of Programming Languages. Christian Collberg

Lemma (x, y, z) is a Pythagorean triple iff (y, x, z) is a Pythagorean triple.

University of Arizona, Department of Computer Science. CSc 453 Assignment 5 Due 23:59, Dec points. Christian Collberg November 19, 2002

Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo

CSc 520 Principles of Programming Languages

Sorting Pearson Education, Inc. All rights reserved.

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

CSc 520 Principles of Programming Languages

Introduction to Programming, Aug-Dec 2006

Chapter 1 Programming: A General Overview

This book is licensed under a Creative Commons Attribution 3.0 License

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

Problem Solving and 'C' Programming

Shell CSCE 314 TAMU. Functions continued

Armstrong Atlantic State University Engineering Studies MATLAB Marina Sorting Primer

Structured Programming. Dr. Mohamed Khedr Lecture 9

Dept. of CSE, IIT KGP

Introduction to Programming: Lecture 3

Functional Programming in Haskell for A level teachers

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

CSCE 314 Programming Languages. Functional Parsers

Introduction to Programming, Aug-Dec 2008

COMP2611: Computer Organization. Data Representation

CS 310 Advanced Data Structures and Algorithms

CS115 - Module 9 - filter, map, and friends

MODULE 2: Branching and Looping

CSc 372 Comparative Programming Languages

CS2104 Prog. Lang. Concepts

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

CSc 520 Principles of Programming Languages

CISC 1100: Structures of Computer Science

Fall Recursion and induction. Stephen Brookes. Lecture 4

What is an algorithm? CISC 1100/1400 Structures of Comp. Sci./Discrete Structures Chapter 8 Algorithms. Applications of algorithms

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

Transcription:

1/20 CSc 372 Comparative Programming Languages 15 : Haskell List Comprehension Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg

2/20 List Comprehensions Haskell has a notation called list comprehension (adapted from mathematics where it is used to construct sets) that is very convenient to describe certain kinds of lists. Syntax: [ expr qualifier, qualifier, ] In English, this reads: Generate a list where the elements are of the form expr, such that the elements fulfill the conditions in the qualifier s. The expression can be any valid Haskell expression. The qualifier s can have three different forms: Generators, Filters, and Local Definitions.

3/20 Generator Qualifiers Generate a number of elements that can be used in the expression part of the list comprehension. Syntax: pattern <- list expr The pattern is often a simple variable. The list expr is often an arithmetic sequence. [n n<-[1..5]] [1,2,3,4,5] [n*n n<-[1..5]] [1,4,9,16,25] [(n,n*n) n<-[1..3]] [(1,1),(2,4),(3,9)]

4/20 Filter Qualifiers A filter is a boolean expression that removes elements that would otherwise have been included in the list comprehension. We often use a generator to produce a sequence of elements, and a filter to remove elements which are not needed. [n*n n<-[1..9],even n] [4,16,36,64] [(n,n*n) n<-[1..3],n<n*n] [(2,4),(3,9)]

5/20 Local Definitions We can define a local variable within the list comprehension. Example: [n*n let n = 2] [4]

6/20 Qualifiers Earlier generators (those to the left) vary more slowly than later ones. Compare nested for-loops in procedural languages, where earlier (outer) loop indexes vary more slowly than later (inner) ones. for i := 1 to 9 do for j := 1 to 3 do print (i, j) Pascal: Haskell: [(i,j) i<-[1..9], j<-[1..3]] [ (1,1),(1,2),(1,3), (2,1),(2,2),(2,3), (9,1),(9,2),(9,3)]

7/20 Qualifiers... Qualifiers to the right may use values generated by qualifiers to the left. Compare Pascal where inner loops may use index values generated by outer loops. for i := 1 to 3 do for j := i to 4 do print (i, j) Pascal: Haskell: [(i,j) i<-[1..3], j<-[i..4]] [ (1,1),(1,2),(1,3),(1,4) (2,2),(2,3),(2,4), (3,3),(3,4)] [n*n n<-[1..10], even n] [4,16,36,64,100]

8/20 Example Define a function doublepos xs that doubles the positive elements in a list of integers. In English: Generate a list of elements of the form 2*x, where the x:s are the positive elements from the list xs. In Haskell: doublepos :: [Int] -> [Int] doublepos xs = [2*x x<-xs, x>0] > doublepos [-1,-2,1,2,3] [2,4,6] Note that xs is a list-valued expression.

9/20 Example Define a function spaces n which returns a string of n spaces. > spaces 10 " " Example: Haskell: spaces :: Int -> String spaces n = [ i <- [1..n]] Note that the expression part of the comprehension is of type Char. Note that the generated values of i are never used.

10/20 Example Define a function factors n which returns a list of the integers that divide n. Omit the trivial factors 1 and n. Examples: factors 5 [] factors 100 [2,4,5,10,20,25,50] In Haskell: factors :: Int -> [Int] factors n = [i i<-[2..n-1], n mod i == 0]

11/20 Example Pythagorean Triads: Generate a list of triples (x,y,z) such that x 2 +y 2 = z 2 and x,y,z n. triads n = [(x,y,z) x<-[1..n], y<-[1..n], z<-[1..n], x^2 + y^2 == z^2] triads 5 [(3,4,5),(4,3,5)]

12/20 Example... We can easily avoid generating duplicates: triads n = [(x,y,z) x<-[1..n], y<-[x..n], z<-[y..n], x^2 + y^2 == z^2] triads 11 [(3,4,5), (6,8,10)]

13/20 Example Making Change Write a function change that computes the optimal (smallest) set of coins to make up a certain amount. Defining available (UK) coins: type Coin = Int coins :: [Coin] coins = reverse (sort [1,2,5,10,20,50,100]) Example: > change 23 [20,2,1] > coins [100,50,20,10,5,2,1] > all change 4 [[2,2],[2,1,1],[1,2,1],[1,1,2],[1,1,1,1]]

14/20 Example Making Change... all change returns all the possible ways of combining coins to make a certain amount. all change returns shortest list first. Hence change becomes simple: change amount = head (all change amount) all change returns all possible (decreasing sequences) of change for the given amount. all change :: Int -> [[Coin]] all change 0 = [[]] all change amount = [ c:cs c<-coins, amount>=c, cs<-all change (amount - c) ]

15/20 Example Making Change... all change works by recursion from within a list comprehension. To make change for an amount amount we 1 Find the largest coin c amount: c<-coins,amount>=c. 2 Find how much we now have left to make change for: amount - c. 3 Compute all the ways to make change from the new amount: cs<-all change (amount - c) 4 Combine c and cs: c:cs.

16/20 Example Making Change... If there is more than one coin c amount, then c<-coins,amount>=c will produce all of them. Each such coin will then be combined with all possible ways to make change from amount - c. coins returns the available coins in reverse order. Hence all change will try larger coins first, and return shorter lists first. all change :: Int -> [[Coin]] all change 0 = [[]] all change amount = [ c:cs c<-coins, amount>=c, cs<-all change (amount - c) ]

17/20 Summary A list comprehension [e q] generates a list where all the elements have the form e, and fulfill the requirements of the qualifier q. q can be a generator x<-list in which case x takes on the values in list one at a time. Or, q can be a a boolean expression that filters out unwanted values.

18/20 Exercise Show the lists generated by the following Haskell list expressions. 1 [n*n n<-[1..10],even n] 2 [7 n<-[1..4]] 3 [ (x,y) x<-[1..3], y<-[4..7]] 4 [ (m,n) m<-[1..3], n<-[1..m]] 5 [j i<-[1,-1,2,-2], i>0, j<-[1..i]] 6 [a+b (a,b)<-[(1,2),(3,4),(5,6)]]

19/20 Exercise Use a list comprehension to define a function neglist xs that computes the number of negative elements in a list xs. neglist :: [Int] -> Int neglist n = Template: Examples: > neglist [1,2,3,4,5] 0 > neglist [1,-3,-4,3,4,-5] 3

20/20 Exercise Use a list comprehension to define a function gensquares low high that generates a list of squares of all the even numbers from a given lower limit low to an upper limit high. Template: gensquares :: Int -> Int -> [Int] gensquares low high = [ ] > gensquares 2 5 [4, 16] > gensquares 3 10 [16, 36, 64, 100] Examples: