List Comprehension - Indiana University Bloomington

Post on 27-May-2022

3 views 0 download

Transcript of List Comprehension - Indiana University Bloomington

ListComprehensionYanHuang

SetComprehensionsInmathematics,thecomprehension notationcanbeusedtoconstructnewsetsfromoldsets.

{x2 | x Î {1...5}}

Theset{1,4,9,16,25}ofallnumbersx2 suchthatxisanelementoftheset{1…5}.

ListsComprehensionsInHaskell,asimilarcomprehensionnotationcanbeusedtoconstructnewlists fromoldlists.

[x^2 | x ¬ [1..5]]

Thelist[1,4,9,16,25]ofallnumbersx^2 suchthatxisanelementofthelist[1..5].

z Theexpressionx¬ [1..5]iscalledagenerator,asitstateshowtogeneratevaluesforx.

z Comprehensionscanhavemultiple generators,separatedbycommas.Forexample:

> [(x,y) | x ¬ [1,2,3], y ¬ [4,5]]

[(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)]

z Changingtheorder ofthegeneratorschangestheorderoftheelementsinthefinallist:

> [(x,y) | y ¬ [4,5], x ¬ [1,2,3]]

[(1,4),(2,4),(3,4),(1,5),(2,5),(3,5)]

z Multiplegeneratorsarelikenestedloops,withlatergeneratorsasmoredeeplynestedloopswhosevariableschangevaluemorefrequently.

> [(x,y) | y ¬ [4,5], x ¬ [1,2,3]]

[(1,4),(2,4),(3,4),(1,5),(2,5),(3,5)]

For example:

x ¬ [1,2,3] is the last generator, so the value of the x component of each

pair changes most frequently.

DependentGenerators

Latergeneratorscandepend onthevariablesthatareintroducedbyearliergenerators.

[(x,y) | x ¬ [1..3], y ¬ [x..3]]

Thelist[(1,1),(1,2),(1,3),(2,2),(2,3),(3,3)]ofallpairsofnumbers(x,y)suchthatx,y are

elementsofthelist[1..3]andy³ x.

Usingadependentgeneratorwecandefinethelibraryfunctionthatconcatenates alistoflists:

Forexample:

> concat [[1,2,3],[4,5],[6]]

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

Usingadependentgeneratorwecandefinethelibraryfunctionthatconcatenates alistoflists:

concat :: [[a]] ® [a]concat xss = [x | xs ¬ xss, x ¬ xs]

Forexample:

> concat [[1,2,3],[4,5],[6]]

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

Guards

Listcomprehensionscanuseguards torestrictthevaluesproducedbyearliergenerators.

[x | x ¬ [1..10], even x]

Thelist[2,4,6,8,10]ofallnumbersxsuchthatxisanelementofthelist

[1..10]andxiseven.

Usingaguardwecandefineafunctionthatmapsapositiveintegertoitslistoffactors:

Forexample:

> factors 15

[1,3,5,15]

factors :: Int ® [Int]factors n =

[x | x ¬ [1..n], n `mod` x == 0]

Usingaguardwecandefineafunctionthatmapsapositiveintegertoitslistoffactors:

Forexample:

> factors 15

[1,3,5,15]

Apositiveintegerisprime ifitsonlyfactorsare1anditself.Hence,usingfactorswecandefineafunctionthatdecidesifanumberisprime:

Forexample:

> prime 15False

> prime 7True

Apositiveintegerisprime ifitsonlyfactorsare1anditself.Hence,usingfactorswecandefineafunctionthatdecidesifanumberisprime:

prime :: Int ® Boolprime n = factors n == [1,n]

Forexample:

> prime 15False

> prime 7True

Usingaguardwecannowdefineafunctionthatreturnsthelistofallprimes uptoagivenlimit:

Forexample:

> primes 40

[2,3,5,7,11,13,17,19,23,29,31,37]

Usingaguardwecannowdefineafunctionthatreturnsthelistofallprimes uptoagivenlimit:

primes :: Int ® [Int]primes n = [x | x ¬ [2..n], prime x]

Forexample:

> primes 40

[2,3,5,7,11,13,17,19,23,29,31,37]

TheZipFunctionAusefullibraryfunctioniszip,whichmapstwoliststoalistofpairsoftheircorrespondingelements.

zip :: [a] ® [b] ® [(a,b)]

Forexample:

> zip [’a’,’b’,’c’] [1,2,3,4]

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

Usingzipwecandefineafunctionreturnsthelistofallpairs ofadjacentelementsfromalist:

Forexample:

> pairs [1,2,3,4]

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

Usingzipwecandefineafunctionreturnsthelistofallpairs ofadjacentelementsfromalist:

Forexample:

pairs :: [a] ® [(a,a)]pairs xs = zip xs (tail xs)

> pairs [1,2,3,4]

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

Usingpairswecandefineafunctionthatdecidesiftheelementsinalistaresorted:

Forexample:

> sorted [1,2,3,4]True

> sorted [1,3,2,4]False

Usingpairswecandefineafunctionthatdecidesiftheelementsinalistaresorted:

Forexample:

sorted :: Ord a Þ [a] ® Boolsorted xs =

and [x £ y | (x,y) ¬ pairs xs]

> sorted [1,2,3,4]True

> sorted [1,3,2,4]False

Usingzipwecandefineafunctionthatreturnsthelistofallpositions ofavalueinalist:

positions :: Eq a Þ a ® [a] ® [Int]

positions x xs =

[i | (x’,i) ¬ zip xs [0..], x == x’]

Forexample:

> positions 0 [1,0,0,1,0,1,1,0][1,2,4,7]

StringComprehensions

Astring isasequenceofcharactersenclosedindoublequotes.Internally,however,stringsarerepresentedaslistsofcharacters.

"abc" :: String

Means[’a’,’b’,’c’]::[Char].

Becausestringsarejustspecialkindsoflists,anypolymorphic functionthatoperatesonlistscanalsobeappliedtostrings.Forexample:

> length "abcde"5

> take 3 "abcde""abc"

> zip "abc" [1,2,3,4]

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

Similarly,listcomprehensionscanalsobeusedtodefinefunctionsonstrings,suchcountinghowmanytimesacharacteroccursinastring:

count :: Char ® String ® Intcount x xs =

length [x’ | x’ ¬ xs, x == x’]

Forexample:

> count ’s’ "Mississippi"4

Exercises

Atriple(x,y,z)ofpositiveintegersiscalledpythagorean ifx2 +y2 =z2.Usingalistcomprehension,defineafunction

(1)

pyths :: Int ® [(Int,Int,Int)]

thatmapsanintegerntoallsuchtripleswithcomponentsin[1..n].Forexample:

> pyths 5[(3,4,5),(4,3,5)]

Apositiveintegerisperfect ifitequalsthesumofallofitsfactors,excludingthenumberitself.Usingalistcomprehension,defineafunction

(2)

perfects :: Int ® [Int]

thatreturnsthelistofallperfectnumbersuptoagivenlimit.Forexample:

> perfects 500

[6,28,496]

(xsi * ysi )åi = 0

n-1

Usingalistcomprehension,defineafunctionthatreturnsthescalarproductoftwolists.

Thescalarproduct oftwolistsofintegersxs andysoflengthnisgivebythesumoftheproductsofthecorrespondingintegers:

(3)