List Comprehension Flashcards

1
Q

Describe the syntax of a list comprehension

A

List comprehension has three parts viz., Output function, binding and predicate. This is expressed as [ | , ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

explain [x*2 | x = 12]

A

for each number x in the range 1..10, output x*2, if the output is greater than or equal to 12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

rule for multiple predicates in a comprehension

A

an element should satisfy all predicates to be included in the output list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give a meaningful name for this function

st = [ c | c ]]

A

removeNonUpperCaseLtrsFromGivenString

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Evaluate [ x*y | x 50]

A

[55,80,100,110]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What will be length of the output list from this comprehension?
[ x*y | x <- [8,10,11]]

A

9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe functionality of this comprehension

length’ xs = sum [1 | _ <- xs]

A

Replace every element in the given list with 1 and calculate sum of it. This produces length of a given list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly