Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

andmap in Haskell?

In Racket, there is a really useful built in function andmap that lets see if a function evaluates to true on every element of a given list, as follows:

> (andmap number? (list 2 4 5))
#t
> (andmap number? (list 2 4 "foo"))
#f

Is there an equivalent of this in Haskell, or do you have to construct it yourself by using map and reduce?

like image 565
setholopolus Avatar asked May 17 '26 18:05

setholopolus


2 Answers

You're looking for all:

> all (>0) [1, 2, 3]
True
> all (>0) [1, -2, 3]
False
like image 139
sepp2k Avatar answered May 21 '26 05:05

sepp2k


you can use all for this purpose

e.g.

> all even [2,4]
True
like image 21
karakfa Avatar answered May 21 '26 04:05

karakfa