learning haskell - florida institute of technologyryan/cse4250/notes/learning_haskell.pdf · there...

227
Learning Haskell 1 Using ghci. Interactivity, directives. 2 Expressions for each of the basic data types: Integer, Float, Bool, Char, () — unit. 3 Tuples. Lists: arithmetic sequences, list comprehensions. 4 Bindings. The let expression. 5 Simple functions. Anonymous functions, functions as data, special declaration syntax, patterns, cases, guards. 6 Using ghc. Writing, compiling, and running a main program, interact. Erik Meijer, OSCON ’09 [14min] Simon Peyton Jones, POP 2003, “Retrospective” [ppt] Ryan Stansifer (CS, Florida Tech) Learning Haskell 28 March 2019 1/3

Upload: others

Post on 10-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Learning Haskell

1 Using ghci. Interactivity, directives.

2 Expressions for each of the basic data types: Integer, Float, Bool,Char, () — unit.

3 Tuples. Lists: arithmetic sequences, list comprehensions.

4 Bindings. The let expression.

5 Simple functions. Anonymous functions, functions as data, specialdeclaration syntax, patterns, cases, guards.

6 Using ghc. Writing, compiling, and running a main program,interact.

Erik Meijer, OSCON ’09 [14min]Simon Peyton Jones, POP 2003, “Retrospective” [ppt]

Ryan Stansifer (CS, Florida Tech) Learning Haskell 28 March 2019 1 / 3

Page 2: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Learning Haskell

All things Haskell: haskell.org

Tutorial: Learn You a Haskell for Great Good! by Miran Lipovaca

Searching for functions: Hoogle

Ryan Stansifer (CS, Florida Tech) Learning Haskell 28 March 2019 2 / 3

Page 3: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Learning Haskell

Higher-order functions

Data structures

Type inference

Ryan Stansifer (CS, Florida Tech) Learning Haskell 28 March 2019 3 / 3

Page 4: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 1 / 76

Page 5: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 1 / 76

Page 6: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 1 / 76

Page 7: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 1 / 76

Page 8: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

GHCI directives

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 2 / 76

Page 9: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 3 / 76

Page 10: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Interactive development

load — edit — reload – test; and repeat

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 4 / 76

Page 11: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Overview to skim quickly, orSummary for later

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 5 / 76

Page 12: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 6 / 76

Page 13: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Integers

〈integer〉 ::= 〈digit〉+

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 7 / 76

Page 14: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Some Integers

7

0

3

567

42

5429723947

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 8 / 76

Page 15: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

The Type of Integers

Integer

Integer

Integer

Integer

Integer

Integer

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 9 / 76

Page 16: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Tuples

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

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

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

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 10 / 76

Page 17: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

(’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) Haskell Lists March 28, 2019 11 / 76

Page 18: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

(Char,Bool)

(Integer,Integer)

(Bool,Integer,Bool,Integer)

(Integer,Integer,Integer)

(Char,Integer,Integer,Integer)

(Integer,(Char,Bool),Char)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 12 / 76

Page 19: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Lists

〈list〉 ::= []

::= 〈expr〉:〈list〉

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 13 / 76

Page 20: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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

1:1:[]

():():[]

True:False:True:[]

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

False:False:True:[]

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 14 / 76

Page 21: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

[Integer]

[Integer]

[()]

[Bool]

[Char] = String

[Bool]

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 15 / 76

Page 22: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 16 / 76

Page 23: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions

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

lambdaformal parameterbody

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 17 / 76

Page 24: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

\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) Haskell Lists March 28, 2019 18 / 76

Page 25: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Integer->Integer

[Integer]->[Integer]

String->String

Char->Integer

Integer->Integer

Double->Double

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 19 / 76

Page 26: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Data, all data exists apart from names. Data

meaningOfLive = 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) Haskell Lists March 28, 2019 20 / 76

Page 27: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 21 / 76

Page 28: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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->sind(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) Haskell Lists March 28, 2019 22 / 76

Page 29: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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

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

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 23 / 76

Page 30: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

The Basics

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

2 Structures: Tuple, lists, functions

3 Text: list of character

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 24 / 76

Page 31: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Integer

143succ 343+45*79-4negate 8

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

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 25 / 76

Page 32: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Integer

min 17 34max 17 34

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

---> 1

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) Haskell Lists March 28, 2019 26 / 76

Page 33: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Integer

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

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

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 27 / 76

Page 34: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 28 / 76

Page 35: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 29 / 76

Page 36: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 30 / 76

Page 37: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Tuples

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

fst (2,’b’)snd (1,’a’)

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

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 31 / 76

Page 38: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

What is a List?

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 32 / 76

Page 39: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 33 / 76

Page 40: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 34 / 76

Page 41: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 35 / 76

Page 42: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 36 / 76

Page 43: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 37 / 76

Page 44: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 38 / 76

Page 45: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 39 / 76

Page 46: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List and 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) Haskell Lists March 28, 2019 40 / 76

Page 47: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Video; 31 minutesPresenter: E Meijer

based on Hutton’s book, chapter 5Channel 9 lectures at YouTube

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 41 / 76

Page 48: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 42 / 76

Page 49: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Haskell List ComprehensionA 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 .Also, there are intriguing language extensions:

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) Haskell Lists March 28, 2019 43 / 76

Page 50: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 44 / 76

Page 51: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 45 / 76

Page 52: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 45 / 76

Page 53: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 45 / 76

Page 54: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 45 / 76

Page 55: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 45 / 76

Page 56: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 45 / 76

Page 57: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 45 / 76

Page 58: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 45 / 76

Page 59: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 45 / 76

Page 60: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 45 / 76

Page 61: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 45 / 76

Page 62: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 46 / 76

Page 63: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 46 / 76

Page 64: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 46 / 76

Page 65: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 46 / 76

Page 66: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 46 / 76

Page 67: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 46 / 76

Page 68: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 46 / 76

Page 69: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 46 / 76

Page 70: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 47 / 76

Page 71: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 47 / 76

Page 72: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 47 / 76

Page 73: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Multiple Generators)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 47 / 76

Page 74: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 48 / 76

Page 75: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 48 / 76

Page 76: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 48 / 76

Page 77: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 48 / 76

Page 78: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 48 / 76

Page 79: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 48 / 76

Page 80: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 48 / 76

Page 81: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 48 / 76

Page 82: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Infinite Lists, Nested)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 48 / 76

Page 83: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 84: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 85: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 86: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 87: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 88: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 89: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 90: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 91: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 92: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 93: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 94: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 95: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 96: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 97: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension (Guards aka Filters)

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 49 / 76

Page 98: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 99: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 100: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 101: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 102: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 103: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 104: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 105: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 106: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 107: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 108: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 109: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 110: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 111: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 112: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 113: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 114: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 115: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 116: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 117: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 118: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 119: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 120: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 121: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 122: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 123: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 124: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 125: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 126: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 127: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 128: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 129: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 130: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 131: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 132: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 133: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension and Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 50 / 76

Page 134: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 51 / 76

Page 135: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 51 / 76

Page 136: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 51 / 76

Page 137: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 51 / 76

Page 138: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 51 / 76

Page 139: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 51 / 76

Page 140: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 51 / 76

Page 141: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

List Comprehension, Pythagorean Triples

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 51 / 76

Page 142: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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 referingto data and not just creating data.

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 52 / 76

Page 143: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 53 / 76

Page 144: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 54 / 76

Page 145: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Syntax of NamesEnclosing 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) Haskell Lists March 28, 2019 55 / 76

Page 146: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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 generaldeclartion 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) Haskell Lists March 28, 2019 56 / 76

Page 147: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 57 / 76

Page 148: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 58 / 76

Page 149: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Syntax of Functions

\ x -> 2 * x

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 59 / 76

Page 150: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Syntax of Functions

backslash; pronounced “lambda”

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

arrow separates teh body of the fucntion from the parameter

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 60 / 76

Page 151: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 61 / 76

Page 152: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 62 / 76

Page 153: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 62 / 76

Page 154: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 62 / 76

Page 155: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 62 / 76

Page 156: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 62 / 76

Page 157: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 63 / 76

Page 158: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Upcoming script

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

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 64 / 76

Page 159: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 65 / 76

Page 160: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 65 / 76

Page 161: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 65 / 76

Page 162: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 65 / 76

Page 163: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 65 / 76

Page 164: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 65 / 76

Page 165: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 65 / 76

Page 166: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 65 / 76

Page 167: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using GHCi with Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 65 / 76

Page 168: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 66 / 76

Page 169: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 170: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 171: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 172: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 173: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 174: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 175: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 176: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 177: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 178: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 179: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 180: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Functions Definitions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 67 / 76

Page 181: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

What Can You Do With a Function?

Integer: get the next integer

Tuple: get the first and the second part

List: get the rest of the lsit

Function: use/apply it to an argument

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 68 / 76

Page 182: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 69 / 76

Page 183: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 184: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 185: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 186: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 187: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 188: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 189: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 190: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 191: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 192: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 193: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 194: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 195: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 196: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 197: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 198: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 199: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 200: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Function Application

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 70 / 76

Page 201: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 71 / 76

Page 202: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Naming Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 72 / 76

Page 203: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Naming Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 72 / 76

Page 204: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Naming Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 72 / 76

Page 205: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Naming Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 72 / 76

Page 206: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Naming Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 72 / 76

Page 207: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Naming Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 72 / 76

Page 208: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Naming Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 72 / 76

Page 209: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Naming Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 72 / 76

Page 210: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Naming Functions

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 72 / 76

Page 211: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 73 / 76

Page 212: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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) Haskell Lists March 28, 2019 74 / 76

Page 213: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Patterns

Think of a formal argmument 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) Haskell Lists March 28, 2019 75 / 76

Page 214: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

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 -- defintion 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) Haskell Lists March 28, 2019 76 / 76

Page 215: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Wildcard Patterns

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

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

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 77 / 76

Page 216: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Definition by cases

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

Ryan Stansifer (CS, Florida Tech) Haskell Lists March 28, 2019 78 / 76

Page 217: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Overview

Everything is digital (all files are binary)

Unix streams and pipes (function composition is important)

What is a program?

GHCI writing a programI compiling a program (also -Wall)I running a program (also +RTS -RTS)

interact: boilerplate to turn a function into a working program

Ryan Stansifer (CS, Florida Tech) Main Program With GHC 28 March 2019 1 / 10

Page 218: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Everything is Digital

Ryan Stansifer (CS, Florida Tech) Main Program With GHC 28 March 2019 2 / 10

Page 219: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Streams and Pipes

Along the Stream by Sharon France

Ryan Stansifer (CS, Florida Tech) Main Program With GHC 28 March 2019 3 / 10

Page 220: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

We know what a function is: it maps an elementin the domain to an element in the range.

When we think of a program as a function wegenerally think of something like the factorialfunction from numbers to numbers.

But this is not a “real” program. A real program reads input and writesoutput.

Limiting ourselves to the important case of programs from US-ASCII textto US-ASCII text, a real program is really a function from astream/file/string of text to a stream/file/string of text.

Ryan Stansifer (CS, Florida Tech) Main Program With GHC 28 March 2019 4 / 10

Page 221: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Input and Output Stream

ec

bhjf

gd

aj

JI

HG

FEDCB

A

Ryan Stansifer (CS, Florida Tech) Main Program With GHC 28 March 2019 5 / 10

Page 222: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Using the Glasgow Haskell Compiler

In Haskell, just like numerous other programming languages, one creates asource program in a text file, one invokes the compiler to create anexecutable file, and then one commands the computer to run theexecutable file on some text input and observe or collect the text output.

$ ghc -Wall -o main Main.hs

$ main < input.txt > output.txt

Ryan Stansifer (CS, Florida Tech) Main Program With GHC 28 March 2019 6 / 10

Page 223: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Warning!

Ask the compiler to help you write a more beautiful program by turning onall warnnigs.

$ ghc -Wall -o main Main.hs

Don’t turn in a program with warnings.

Ryan Stansifer (CS, Florida Tech) Main Program With GHC 28 March 2019 7 / 10

Page 224: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

How do do we create a Haskell program that reads the input and writesthe output?

Ryan Stansifer (CS, Florida Tech) Main Program With GHC 28 March 2019 8 / 10

Page 225: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Some programming languages have elaborate IO mechanisms which onemust learn to simple process the the input and produce the output.In Haskell the simplest approach is to use the function interact totransform the conceptually pure program (a function from the input streamto the output stream) to an actual, working program on the computer.

Ryan Stansifer (CS, Florida Tech) Main Program With GHC 28 March 2019 9 / 10

Page 226: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Ryan Stansifer (CS, Florida Tech) Main Program With GHC 28 March 2019 10 / 10

Page 227: Learning Haskell - Florida Institute of Technologyryan/cse4250/notes/learning_haskell.pdf · There are no mutable arrays in Haskell, so lists are the \go to" data structure for collections

Examples On-Line

README

Ryan Stansifer (CS, Florida Tech) Main Program With GHC 28 March 2019 11 / 10