Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Are list comprehensions higher order functions?

I have an assignment where we are not supposed to use any higher order functions, could a list comprehension be classified as a higher order function?

like image 786
tail_recursion Avatar asked Dec 08 '22 04:12

tail_recursion


1 Answers

A list comprehension in Haskell is just syntactic sugar. It is defined in the Haskell 2010 Report here like so:

[ e | True ]          = [e]
[ e | q ]             = [ e | q, True ]
[ e | b, Q  ]         = if b then [ e | Q ] else []
[ e | p <- l, Q ]     = let ok p = [ e | Q ]
                            ok _ = []
                        in concatMap ok  l
[ e | let decls, Q ]  = let decls in [ e | Q ]

Note the use of Q, which ranges over qualifiers (which can include functions), in the patterns. So the answer is yes, a list comprehension is higher-order.

like image 147
d3dave Avatar answered Jan 18 '23 21:01

d3dave