PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

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

Shell CSCE 314 TAMU. Haskell Functions

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

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

Shell CSCE 314 TAMU. Functions continued

Lecture 2: List algorithms using recursion and list comprehensions

Functional Programming in Haskell Part I : Basics

INTRODUCTION TO FUNCTIONAL PROGRAMMING

A general introduction to Functional Programming using Haskell

Lecture 19: Functions, Types and Data Structures in Haskell

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

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

CSc 372 Comparative Programming Languages

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

Introduction to Programming, Aug-Dec 2006

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

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

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Programming Language Concepts: Lecture 14

Haskell An Introduction

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

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

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

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

Standard prelude. Appendix A. A.1 Classes

CS 320: Concepts of Programming Languages

More fun with recursion

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

INTRODUCTION TO HASKELL

Course year Typeclasses and their instances

Programming Languages 3. Definition and Proof by Induction

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

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

A Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16

An introduction introduction to functional functional programming programming using usin Haskell

CS 320: Concepts of Programming Languages

Programming Languages Fall 2013

Haskell through HUGS THE BASICS

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

Introduction to Programming: Lecture 6

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

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

Introduction to Programming: Lecture 3

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Lecture 4: Higher Order Functions

LECTURE 16. Functional Programming

Functional Programming and Haskell

1. (34 points) Suppose that the following definitions are made: (a) (22 points) Give the values of the following expressions.

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

Exercise 1 ( = 24 points)

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

Haskell-Tutorial. Damir Medak Gerhard Navratil. Institute for Geoinformation Technical University Vienna. February 2003

Functional Programming for Logicians - Lecture 1

Topic 9: Type Checking

Topic 9: Type Checking

CS 320 Midterm Exam. Fall 2018

Haskell Types COMP360

! " Haskell Expressions are treated as: !" Thunks. !" Closures. !" Delayed Computations. !" Suspensions. ! " Expressions are evaluated: !

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

Imperative languages

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

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

An Introduction to Programming in Haskell. Gabriel J. Ferrer Hendrix College

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

Abstract Types, Algebraic Types, and Type Classes

Set Haskell Exercises

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

A tour of the Haskell Prelude

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

List Functions, and Higher-Order Functions

Module Title: Informatics 1 Functional Programming (first sitting) Exam Diet (Dec/April/Aug): December 2014 Brief notes on answers:

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

Type-indexed functions in Generic Haskell

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

CS 457/557: Functional Languages

An introduction to functional programming. July 23, 2010

COMPUTER SCIENCE 123. Foundations of Computer Science. 5. Strings

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

Logic - CM0845 Introduction to Haskell

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Informatics 1 Functional Programming Lecture 5. Function properties. Don Sannella University of Edinburgh

Advances in Programming Languages

Functional Programming in Haskell for A level teachers

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

Programming Paradigms

Extended Static Checking for Haskell (ESC/Haskell)

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

Exercise 1 ( = 22 points)

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

Programming in Haskell Aug-Nov 2015

Exercises on ML. Programming Languages. Chanseok Oh

Advanced features of Functional Programming (Haskell)

1 The smallest free number

Logical Methods in... using Haskell Getting Started

Higher-Order Functions

SML A F unctional Functional Language Language Lecture 19

Transcription:

PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions 0

Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x 2 x {1...5}} The set {1,4,9,16,25} of all numbers x 2 such that x is an element of the set {1 5}. 1

Lists Comprehensions In Haskell, a similar comprehension notation can be used to construct new lists from old lists. [x^2 x [1..5]] The list [1,4,9,16,25] of all numbers x^2 such that x is an element of the list [1..5]. 2

Note: The expression x [1..5] is called a generator, as it states how to generate values for x. Comprehensions can have multiple generators, separated by commas. For example: > [(x,y) x [1,2,3], y [4,5]] [(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)] 3

Changing the order of the generators changes the order of the elements in the final list: > [(x,y) y [4,5], x [1,2,3]] [(1,4),(2,4),(3,4),(1,5),(2,5),(3,5)] Multiple generators are like nested loops, with later generators as more deeply nested loops whose variables change value more frequently. 4

For example: > [(x,y) y [4,5], x [1,2,3]] [(1,4),(2,4),(3,4),(1,5),(2,5),(3,5)] x [1,2,3] is the last generator, so the value of the x component of each pair changes most frequently. 5

Dependant Generators Later generators can depend on the variables that are introduced by earlier generators. [(x,y) x [1..3], y [x..3]] The list [(1,1),(1,2),(1,3),(2,2),(2,3),(3,3)] of all pairs of numbers (x,y) such that x,y are elements of the list [1..3] and y x. 6

Using a dependant generator we can define the library function that concatenates a list of lists: concat :: [[a]] [a] concat xss = [x xs xss, x xs] For example: > concat [[1,2,3],[4,5],[6]] [1,2,3,4,5,6] 7

Guards List comprehensions can use guards to restrict the values produced by earlier generators. [x x [1..10], even x] The list [2,4,6,8,10] of all numbers x such that x is an element of the list [1..10] and x is even. 8

Using a guard we can define a function that maps a positive integer to its list of factors: factors :: Int [Int] factors n = [x x [1..n], n `mod` x == 0] For example: > factors 15 [1,3,5,15] 9

A positive integer is prime if its only factors are 1 and itself. Hence, using factors we can define a function that decides if a number is prime: prime :: Int Bool prime n = factors n == [1,n] For example: > prime 15 False > prime 7 True 10

Using a guard we can now define a function that returns the list of all primes up to a given limit: primes :: Int [Int] primes n = [x x [2..n], prime x] For example: > primes 40 [2,3,5,7,11,13,17,19,23,29,31,37] 11

The Zip Function A useful library function is zip, which maps two lists to a list of pairs of their corresponding elements. zip :: [a] [b] [(a,b)] For example: > zip [ a, b, c ] [1,2,3,4] [( a,1),( b,2),( c,3)] 12

Using zip we can define a function returns the list of all pairs of adjacent elements from a list: pairs :: [a] [(a,a)] pairs xs = zip xs (tail xs) For example: > pairs [1,2,3,4] [(1,2),(2,3),(3,4)] 13

Using pairs we can define a function that decides if the elements in a list are sorted: sorted :: Ord a [a] Bool sorted xs = and [x y (x,y) pairs xs] For example: > sorted [1,2,3,4] True > sorted [1,3,2,4] False 14

Using zip we can define a function that returns the list of all positions of a value in a list: positions :: Eq a a [a] [Int] positions x xs = [i (x,i) zip xs [0..], x == x ] For example: > positions 0 [1,0,0,1,0,1,1,0] [1,2,4,7] 15

String Comprehensions A string is a sequence of characters enclosed in double quotes. Internally, however, strings are represented as lists of characters. "abc" :: String Means [ a, b, c ] :: [Char]. 16

Because strings are just special kinds of lists, any polymorphic function that operates on lists can also be applied to strings. For example: > length "abcde" 5 > take 3 "abcde" "abc" > zip "abc" [1,2,3,4] [( a,1),( b,2),( c,3)] 17

Similarly, list comprehensions can also be used to define functions on strings, such counting how many times a character occurs in a string: count :: Char String Int count x xs = length [x x xs, x == x ] For example: > count s "Mississippi" 4 18

Exercises (1) A triple (x,y,z) of positive integers is called pythagorean if x 2 + y 2 = z 2. Using a list comprehension, define a function pyths :: Int [(Int,Int,Int)] that maps an integer n to all such triples with components in [1..n]. For example: > pyths 5 [(3,4,5),(4,3,5)] 19

(2) A positive integer is perfect if it equals the sum of all of its factors, excluding the number itself. Using a list comprehension, define a function perfects :: Int [Int] that returns the list of all perfect numbers up to a given limit. For example: > perfects 500 [6,28,496] 20

(3) The scalar product of two lists of integers xs and ys of length n is give by the sum of the products of the corresponding integers: n-1 (xs i * ys i ) i = 0 Using a list comprehension, define a function that returns the scalar product of two lists. 21