List Comprehension Flashcards
Describe the syntax of a list comprehension
List comprehension has three parts viz., Output function, binding and predicate. This is expressed as [ | , ]
explain [x*2 | x = 12]
for each number x in the range 1..10, output x*2, if the output is greater than or equal to 12
rule for multiple predicates in a comprehension
an element should satisfy all predicates to be included in the output list
Give a meaningful name for this function
st = [ c | c ]]
removeNonUpperCaseLtrsFromGivenString
Evaluate [ x*y | x 50]
[55,80,100,110]
What will be length of the output list from this comprehension?
[ x*y | x <- [8,10,11]]
9
Describe functionality of this comprehension
length’ xs = sum [1 | _ <- xs]
Replace every element in the given list with 1 and calculate sum of it. This produces length of a given list