-1

I would like to return a list of Bools where each responds to a certain check, here is a javascript example:

if(x == 2)
    a = false;
if(x == 3)
    b = false;
if(y == 2)
    c = false;
if(y == 3)
    d = false;

return [a, b, c, d];

Dirty example, I know. Just wondering what a good approach might be.

2 Answers2

1
x=5
y=3

elementary

zipWith (==) [x,x,y,y] [2,3,2,3]
[False,False,False,True]

or with magic

import Control.Monad (liftM2)
liftM2 (==) [x,y] [2,3]
[False,False,False,True]
karakfa
  • 66,216
  • 7
  • 41
  • 56
0

you can always use

f :: (Eq a, Num a,Eq b, Num b) => a -> b -> [Bool]
f x y = [x == 2, x == 3, y == 2, y == 3]

if you have only one parameter and multiple predicates you can do something like

f x = map ($ x) [p1,p2,p3,p4]
 where p1 = odd
       p2 = even
       p3 = (==3)
       p4 = (==4)

which lends itself good to creating the checks programmatically another trick is to use it the other way around

f x = map (==x) [1..10]

or when you have a list of xs you can use zipWith (==) xs [1..].

epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74