outline - cs.fit.eduryan/cse4250/notes/master2.pdf · outline 1 ghci 2 basic expressions and...

226
Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension 5 Basic functions 6 Patterns 7 Type variables. When Haskell does not care what the type is, a type variable is used to stand for any type 8 Higher-order functions, partial application 9 Map, filter, fold 10 Curry, uncurry, isomorphism 11 Data types 12 Main program, interact, compilation Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 1 / 86

Upload: others

Post on 20-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Outline

1 GHCI

2 Basic expressions and primitive data types. Integers, reals, characters,unit.

3 Tuples

4 Lists, list comprehension

5 Basic functions

6 Patterns

7 Type variables. When Haskell does not care what the type is, a typevariable is used to stand for any type

8 Higher-order functions, partial application

9 Map, filter, fold

10 Curry, uncurry, isomorphism

11 Data types

12 Main program, interact, compilation

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 1 / 86

Page 2: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 2 / 86

Page 3: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 2 / 86

Page 4: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 2 / 86

Page 5: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 2 / 86

Page 6: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

GHCI directives

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 3 / 86

Page 7: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

GHCI directives

<statement> evaluate/run <statement>

: repeat last command

:{\n ..lines.. \n:}\n multiline command

:add [*]<module> ... add module(s) to the current target set

:browse[!] [[*]<mod>] display the names defined by module <mod>

(!: more details; *: all top-level names)

:cd <dir> change directory to <dir>

:complete <dom> [<rng>] <s> list completions for partial input string

:help, :? display this list of commands

:info[<name> ...] display information about the given names

:kind <type> show the kind of <type>

:load [*]<module> ... load module(s) and their dependents

:main [<arguments> ...] run the main function with the given arguments

:module [+/-] [*]<mod> ... set the context for expression evaluation

:quit exit GHCi

:reload reload the current module set

:type <expr> show the type of <expr>

:type +d <expr> show the type of <expr>, defaulting type variables

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 4 / 86

Page 8: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Interactive development

load — edit — reload – test; and repeat

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 5 / 86

Page 9: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Overview to skim quickly, orSummary for later

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 6 / 86

Page 10: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Data

We typically view programming as a combination of algorithms(procedures) and data structures.

Functional programming presents us with a grand unified theory of dataand procedures. This may be unsettling at first.

Integers, lists, tuples, functions: they are all data.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 7 / 86

Page 11: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Data

Integers

〈integer〉 ::= 〈digit〉+

Tuples

〈tuple〉 ::= (〈expr〉,〈expr〉)

::= (〈expr〉,〈expr〉,〈expr〉)

::= (〈expr〉,〈expr〉,〈expr〉,〈expr〉)

Lists

〈list〉 ::= []

::= 〈expr〉:〈list〉

Functions

〈function〉 ::= \〈name〉->〈expr〉

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 8 / 86

Page 12: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Integers

〈integer〉 ::= 〈digit〉+

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 9 / 86

Page 13: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Some Integers

7

0

3

567

42

5429723947

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 10 / 86

Page 14: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

The Type of Integers

Integer

Integer

Integer

Integer

Integer

Integer

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 11 / 86

Page 15: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Integer literals are canonical values.

Integer expressions might be evaluated/reduced to integer canonicalvalues.

More of the usual details about integers later, but now quickly on to tuples.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 12 / 86

Page 16: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Integer literals are canonical values.

Integer expressions might be evaluated/reduced to integer canonicalvalues.

More of the usual details about integers later, but now quickly on to tuples.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 12 / 86

Page 17: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Tuples

〈tuple〉 ::= (〈expr〉,〈expr〉)

::= (〈expr〉,〈expr〉,〈expr〉)

::= (〈expr〉,〈expr〉,〈expr〉,〈expr〉)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 13 / 86

Page 18: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

(’a’,True)

(5,3+4)

(True,2,False,3)

(2+3,2*3,2-3)

(’z’,1,1,1)

(4*7,(’a’,True),’p’)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 14 / 86

Page 19: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

(Char,Bool)

(Integer,Integer)

(Bool,Integer,Bool,Integer)

(Integer,Integer,Integer)

(Char,Integer,Integer,Integer)

(Integer,(Char,Bool),Char)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 15 / 86

Page 20: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Lists

〈list〉 ::= []

::= 〈expr〉:〈list〉

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 16 / 86

Page 21: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

3:5:(3+6):7:[]

1:1:[]

():():[]

True:False:True:[]

’a’:’b’:’c’:[]

False:False:True:[]

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 17 / 86

Page 22: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

[Integer]

[Integer]

[()]

[Bool]

[Char] = String

[Bool]

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 18 / 86

Page 23: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Lists Have Special Syntax

[3,5,3+6,7]

[1,1]

[(),()]

[True,False,True]

[’a’,’b’,’c’] = "abc"

[False,False,True]

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 19 / 86

Page 24: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions

〈function〉 ::= \〈name〉->〈expr〉

1 lambda (backslash); punctuation, keyword

2 formal parameter; an identifier (really a pattern)

3 “arrow” (->); punctuation, keyword

4 body; arbitrary expression possibly using the identifier

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 20 / 86

Page 25: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

\i ->i+2

\xs ->2:xs

\s ->s++", "++s

\c ->ord c

\i ->max i 0

\x ->sin (x+pi)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 21 / 86

Page 26: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Integer->Integer

[Integer]->[Integer]

String->String

Char->Integer

Integer->Integer

Double->Double

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 22 / 86

Page 27: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Why do people name things? So that they can refer to the later. Data, alldata, exists apart from thier name.

theMeaningOfLife = 42

myVerySpecialPair = (’a’, 0)

myShortList = [True ,False ,True]

add2 = \ i -> i+2

cons2 = \ xs -> 2:xs

double = \ s -> s ++ ", " ++ s

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 23 / 86

Page 28: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Declarations Have Special Syntax

add2 i = i+2

cons2 xs = 2:xs

doubleWord s = s ++ ", " ++ s

toOrd c = ord c

cutOff i = max i 0

f x = sin (x+pi)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 24 / 86

Page 29: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Declarations Have Special Syntax

The special syntax for function is so obvious and familiar and using inreductions so obvious and familiar, it hardly needs mentioning. But wewait for later to elaborate.

What is the canonical value of a function? Itself!

We by observing that these four kinds of data and be combined in endlesswithout constraint.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 25 / 86

Page 30: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Declarations Have Special Syntax

The special syntax for function is so obvious and familiar and using inreductions so obvious and familiar, it hardly needs mentioning. But wewait for later to elaborate.

What is the canonical value of a function?

Itself!

We by observing that these four kinds of data and be combined in endlesswithout constraint.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 25 / 86

Page 31: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Declarations Have Special Syntax

The special syntax for function is so obvious and familiar and using inreductions so obvious and familiar, it hardly needs mentioning. But wewait for later to elaborate.

What is the canonical value of a function? Itself!

We by observing that these four kinds of data and be combined in endlesswithout constraint.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 25 / 86

Page 32: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Declarations Have Special Syntax

The special syntax for function is so obvious and familiar and using inreductions so obvious and familiar, it hardly needs mentioning. But wewait for later to elaborate.

What is the canonical value of a function? Itself!

We by observing that these four kinds of data and be combined in endlesswithout constraint.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 25 / 86

Page 33: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Complex Canonical Values

(2,(’a’,5), "abc", True) :: (Integer , (Char ,Integer), [Char], Bool)([1],(’a’,5), "abc", True , "xyz") :: ([ Integer], (Char ,Integer), [Char], Bool , [Char])( \i->i+2 , \x->sin(x+pi) ) :: (Integer ->Integer , Double ->Double)

[(1,True ,"abc"), (2,True ,"mno"), (3,True ,"xyz")] :: [(Integer , Book , [Char ])]

\ i -> (i,i) :: Integer -> (Integer ,Integer)\ p -> (fst p + snd p, fst p * snd p, fst p - snd p) :: (Integer ,Integer) -> (Integer ,Integer , Integer)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 26 / 86

Page 34: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

End of the quick overview(proceed to basics), or

End of summary(proceed to writing a program with GHC)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 27 / 86

Page 35: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

The Basics

1 Primitive: Integer, floating-point, character, boolean, unit

2 Structures: Tuple, lists, functions

3 Text: lists of character

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 28 / 86

Page 36: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Integer

143succ 343+45*79-4negate 8

---- watch out: 3 * -8 NO!3 * (-8)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 29 / 86

Page 37: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Integer

min 17 34max 17 34

div 24 724 ‘div ‘ 724 ‘rem ‘ 7mod 36 5

---> 1

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 30 / 86

Page 38: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Integer

quot 36 5---> 7

div 36 5---> 7

quot 36 (-5)---> -7

div 36 (-5)---> -8

rem 36 5---> 1

mod 36 5---> 1

rem 36 (-5)---> 1

mod 36 (-5)---> -4

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 31 / 86

Page 39: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Integer

quotRem 36 (-5)---> (-7,1)

divMod 36 (-5)---> (-8,-4)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 32 / 86

Page 40: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Floating-Point

--- pi,exp ,sqrt ,log ,(**),sin ,tan ,cos ,--- truncate , ceiling , round , truncate , floor3.2 + 43.15.2 * 43.23452369.9 - 2.32345.2345 / 34.3434.4 ^ 344254 ** 4.345sqrt (4.59)sin (1.7172)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 33 / 86

Page 41: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Char

’a’

--- :browse Data.Charord :: Char -> Intchr :: Int -> ChardigitToInt :: Char -> IntintToDigit :: Int -> ChartoLower :: Char -> ChartoUpper :: Char -> CharisAlphaNum :: Char -> BoolisDigit :: Char -> BoolisAlpha :: Char -> BoolisLower :: Char -> BoolisUpper :: Char -> BoolisSpace :: Char -> Bool

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 34 / 86

Page 42: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Bool

FalseTrueFalse && TrueFalse || Truenot True-- otherwise is defined to be the value True-- for the purposes of making guards more readableotherwise

if True then 4 else 5

-- important predicates-- (==), (/=), (<), (>=), (>), (<=), compare

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 35 / 86

Page 43: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Boolean Implication

Because boolean values are ordered we use the less-than-or-equals relationas the boolean connective for implication!

False <= False -- F -> F = TFalse <= True -- F -> T = TTrue <= False -- T -> F = FTrue <= True -- T -> T = T

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 36 / 86

Page 44: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Tuples

(2, ’a’) -- a pair(4.34 , ’a’, 456) -- a triple(True , (), 1)(2, "ab", 2*2, 5.0)( 2 ) -- not a tuple((())) -- unit , not a tuple either

fst (2,’b’) -- pair projectionsnd (1,’a’) -- pair projection

-- pairs of pairs(2,(’a’,3,4),"abcd")((2,3,4), (True , 3.3))

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 37 / 86

Page 45: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

What is a List?

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 38 / 86

Page 46: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

How do you make a List in Haskell?

Two constructors “nil” and “cons”

[]

1 : []

1 : (2 : [])

1 : (2 : (3 : []))

1 : (2 : (3 : (4 : [])))

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 39 / 86

Page 47: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

How do you make a List in Haskell?

“Cons” is right associative; and, anyway, lists have special syntax.

[]

1:[]

1 : (2 : [])

1 : (2 : (3 : []))

1 : (2 : (3 : (4:[])))

[]

1:[]

1:2:[]

1:2:3:[]

1:2:3:4:[]

[]

[1]

[1,2]

[1,2,3]

[1,2,3,4]

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 40 / 86

Page 48: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 41 / 86

Page 49: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 42 / 86

Page 50: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

What can you do with a list?

head [1,2,3,4]

tail [1,2,3,4]

null [1,2,3,4]

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 43 / 86

Page 51: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Lists are polymorphic but homogeneous.

[1,2,3,4]

[’a’, ’b’, ’c’]

[(1,’a’), (2,’b’), (3,’c’)]

[ [], [1], [1,2,3,4], [2 ,3]]

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 44 / 86

Page 52: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

There are no mutable arrays in Haskell, so lists are the “go to” datastructure for collections.

Every list function one can think up has been pre-defined in Haskell. Seelists at Wiki Haskell.

length(++) -- appendelem -- member(!!) -- get elementconcat -- flattenlast initsplitAttake dropsort nubreversesum productminimum maximumand orall any

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 45 / 86

Page 53: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Efficiency not determined by the letters in the name.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 46 / 86

Page 54: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Arithmetic Sequences

[-1,-1 .. 0][3,2 .. 8][0,2 .. 1][1,1 .. 1][3,3 .. (-4)][2,1 .. (-4)][3,3 ..( -12)][-1,-1 .. 8][-6,-6 ..12][1 ,2..( -12)][-6,-5 .. (-4)][1,0 .. 5][-6,-6 .. 8][-1,-2 .. ( -4)][1,2 .. 5]

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 47 / 86

Page 55: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Chapter 5: List ComprehensionPresenter: Erik Meijer

Video; 31 minutesbased on Hutton’s book

Channel 9 lectures at

“Good stuff comes from Math”“Sets are not computationally good”

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 48 / 86

Page 56: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Defining sets by their properties is sometimes known as set comprehensionand found often in mathematical texts. For example,

{x ∈ R | x > 0}

In mathematical notation we write

{ x2 | x ∈ {1, 2, . . . , 5} }

to mean the set {1, 4, 9, 16, 25}.In Haskell a similar syntax is used for lists. The special square bracketssyntax is expanded to include generators and filters.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 49 / 86

Page 57: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Haskell List Comprehension

A list comprehension has the form:

[ expr | qual1 , . . . , qualn ]

where 1 ≥ n

There are three types qualifiers

generators of the form pat<-expr, where p is a pattern (see Section3.17) of type t and e is an expression of type [t]

local bindings that provide new definitions for use in the generatedexpression expr or subsequent boolean guards and generators

boolean guards, which are arbitrary expressions of type Bool.

See the section on List Comprehensions in the Haskell 2010 LanguageReport .

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 50 / 86

Page 58: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Also, there are intriguing language extensions to list comprehension thatresemble more complex database query languages.

ParallelListComp. Syntax: [ expr | qualifier, ... |

qualifier, ... ]

TransformListComp. Three new keywords: group, by, and using.

See a paper by Simon Peyton Jones.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 51 / 86

Page 59: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Upcoming script

[ n | n <- [1..5] ][ (n+2,5*n) | n <-[1..3] ] -- list of pairs:mod + Data.Char[ toUpper c | c <- "one fish" ]

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 52 / 86

Page 60: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 53 / 86

Page 61: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 53 / 86

Page 62: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 53 / 86

Page 63: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 53 / 86

Page 64: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 53 / 86

Page 65: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 53 / 86

Page 66: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 53 / 86

Page 67: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 53 / 86

Page 68: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 53 / 86

Page 69: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 53 / 86

Page 70: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 53 / 86

Page 71: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 54 / 86

Page 72: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 54 / 86

Page 73: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 54 / 86

Page 74: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 54 / 86

Page 75: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 54 / 86

Page 76: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 54 / 86

Page 77: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 54 / 86

Page 78: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 54 / 86

Page 79: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 55 / 86

Page 80: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 55 / 86

Page 81: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 55 / 86

Page 82: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 55 / 86

Page 83: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 56 / 86

Page 84: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 56 / 86

Page 85: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 56 / 86

Page 86: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 56 / 86

Page 87: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 56 / 86

Page 88: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 56 / 86

Page 89: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 56 / 86

Page 90: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 56 / 86

Page 91: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 56 / 86

Page 92: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 93: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 94: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 95: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 96: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 97: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 98: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 99: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 100: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 101: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 102: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 103: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 104: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 105: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 106: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 57 / 86

Page 107: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 108: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 109: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 110: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 111: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 112: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 113: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 114: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 115: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 116: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 117: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 118: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 119: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 120: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 121: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 122: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 123: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 124: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 125: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 126: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 127: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 128: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 129: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 130: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 131: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 132: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 133: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 134: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 135: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 136: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 137: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 138: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 139: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 140: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 141: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 142: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 58 / 86

Page 143: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 59 / 86

Page 144: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 59 / 86

Page 145: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 59 / 86

Page 146: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 59 / 86

Page 147: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 59 / 86

Page 148: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 59 / 86

Page 149: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 59 / 86

Page 150: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 59 / 86

Page 151: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Before continuing on the last and most important data value: functions,we consider the problem of giving data values names.

It is important psychologically to give the programmer a means of referringto data and not just creating data.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 60 / 86

Page 152: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Every object of computation/value gets a name in the same way:

theMeaningOfLife = 42

NB. Not assignment!

Control of scope using let expression.

let <declaration > in <expression >

[We don’t need any examples now.]

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 61 / 86

Page 153: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Syntax of Names

Names consist of either letters and digits, or of all symbols. All symbolicnames are treated as infix operators by the parser. All non-symbolic namesare treated as prefix functions by the parser.

:!!++*

divquotRemInteger

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 62 / 86

Page 154: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Syntax of Names

Enclosing a name with () tells the parser to drop the infix assumption.Enclosing a name with backquotes tells the parser to assume infixassumption.

prefix infix

alphabetic elem ‘elem‘

symbolc (+) +

div 24 724 ‘div ‘ 7

elem 5 [1,2,3,4,5,6]5 ‘elem ‘ [1,2,3,4,5,6]

(+) 2 32 + 3

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 63 / 86

Page 155: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Declarations

A name is given a value by a declaration of the simple form:

name = value

In Haskell this declaration is actually a specific case of a more generaldeclaration of the form:

pattern "=" value

Patterns (section 3.17.1 of the reference 2010 manual) are more naturallydiscussed later in the context of functions.

There are declarations for data types and classes. These will be discussedlater.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 64 / 86

Page 156: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Declarations

Many languages have declarations for many kinds of things: constants,variables, procedures, and functions.

Haskell does not talk about memory locations (however, see boxed versusunboxed), hence no need for variable declarations.

Fundamentally, given a datum a name in Haskell is the same for anyvalue—functions included.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 65 / 86

Page 157: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Overview of Simple Functions

1 syntax (lambda)

2 Two Haskell quirks

3 anonymous (functions as regular data)

4 special syntax

5 patterns (generalization of formal parameters)

6 definition by cases

7 guards [omit]

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 66 / 86

Page 158: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Syntax of Functions

\ x -> 2 * x

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 67 / 86

Page 159: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Syntax of Functions

backslash; pronounced “lambda”

identifier: name of formal parameter (but generalized to patternslater!)

arrow separates the body of the function from the parameter

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 68 / 86

Page 160: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Before we can begin illustrating functions we must deal with two quirks inHaskell which totally distract and even obscure the main issues.

1 Haskell does not print anything reasonable to represent a function bydefault.

2 The types of many function contain class constraints, and there is noneed to discuss classes in a simple introduction to Haskell. (Use:type +d directive.)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 69 / 86

Page 161: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 70 / 86

Page 162: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 70 / 86

Page 163: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 70 / 86

Page 164: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 70 / 86

Page 165: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 70 / 86

Page 166: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Haskell magical show function will show/print anything, but not functions.

In Haskell’s defense, what is the printable representation of a function? Isthe Intel assembly code? The abstract syntax tree of the code? Should itjust parrot back out the input source code?

One can get the Haskell interactive system to print the word <function>

which seems like a really better idea than one of its inscrutable errormessages.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 71 / 86

Page 167: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Upcoming script

:type ’A’:type 3:type \ b -> not b:type \ x -> not x:set +t\ x -> x + 1

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 72 / 86

Page 168: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 73 / 86

Page 169: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 73 / 86

Page 170: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 73 / 86

Page 171: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 73 / 86

Page 172: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 73 / 86

Page 173: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 73 / 86

Page 174: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 73 / 86

Page 175: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 73 / 86

Page 176: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 73 / 86

Page 177: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Are Data

Upcoming script

:mod + Text.Show.Functions Data.Char

\ x -> x + 1

\ i -> max i 0

\ x -> sin (x+pi)

\ c -> prd c

\ s -> s ++ ", " ++ s

\ xs -> 2:xs

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 74 / 86

Page 178: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 179: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 180: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 181: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 182: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 183: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 184: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 185: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 186: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 187: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 188: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 189: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 75 / 86

Page 190: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

What Can You Do With a Function?

This questions is easy for the usual data types:

Integer: get the next integer

Pari: get the first and the second part

List: get head and the rest of the list

Function: use/apply it to an argument

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 76 / 86

Page 191: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

What Can You Do With a Function?

This questions is easy for the usual data types:

Integer: get the next integer

Pari: get the first and the second part

List: get head and the rest of the list

Function: use/apply it to an argument

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 76 / 86

Page 192: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions Application

We ask what is the type of the function, and then we apply it to someelement of the domain.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 77 / 86

Page 193: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 194: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 195: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 196: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 197: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 198: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 199: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 200: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 201: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 202: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 203: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 204: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 205: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 206: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 207: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 208: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 209: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 210: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Application

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 78 / 86

Page 211: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

How do functions get names?The same way anything gets a name!

add1 = \ x -> x + 1

cutOff = \ i -> max i 0

g = \ x -> sin (x+pi)

f = \ c -> ord c

double = \ s -> s ++ ", " ++ s

cons2 = \ xs -> 2:xs

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 79 / 86

Page 212: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Naming Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 80 / 86

Page 213: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Naming Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 80 / 86

Page 214: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Naming Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 80 / 86

Page 215: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Naming Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 80 / 86

Page 216: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Naming Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 80 / 86

Page 217: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Naming Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 80 / 86

Page 218: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Naming Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 80 / 86

Page 219: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Naming Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 80 / 86

Page 220: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Naming Functions

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 80 / 86

Page 221: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Function Declaration Has Special Syntax

add1 = \ x -> x + 1

cutOff = \ i -> max i 0

g = \ x -> sin (x+pi)

f = \ c -> ord c

double =\s->s++", "++ s

cons2 = \ xs -> 2:xs

add1 x = x + 1

cutOff i = max i 0

g x = sin (x+pi)

f c = ord c

double s = s++", "++s

cons2 xs = 2:xs

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 81 / 86

Page 222: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Functions

f x = if null x then "Empty!" else "Not Empty!"

factorial n = if n<2 then 1 else n * factorial (n-1)

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 82 / 86

Page 223: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Patterns

Think of a formal argument as a name that matches any value in thedomain of the function.

Patterns as formal arguments use constructors to match against the valuegiven to the function as the actual argument.

If the value does not match, you are out of luck.

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 83 / 86

Page 224: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Patterns

p3 (x,y,z) = z -- definition of function p3

p3 (1,2,3) -- evaluates to 3

tr = (1,2,3)

p3 tr -- evaluates to 3

f (x:2:y:rest) = x+y -- definition of f

f (1:2:3:9:[]) -- evaluates to 4

f [1,2,3,9] -- evaluates to 4

l = [12,3,9]

f l -- evaluates to 4

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 84 / 86

Page 225: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Wildcard Patterns

p3 (_,_,z) = z -- definition of function p3

f (_:2:_:_) = x+y -- definition of f

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 85 / 86

Page 226: Outline - cs.fit.eduryan/cse4250/notes/master2.pdf · Outline 1 GHCI 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples 4 Lists, list comprehension

Definition by cases

f [] = "empty"f (x:[]) = "single"f (x:y:[]) = "small"f (x:y:z:[]) = "medium"f (_) = "large"

Ryan Stansifer (CS, Florida Tech) Learning Haskell April 9, 2020 86 / 86