Jason Born
Jason Born

Reputation: 173

Haskell: Using 'map' within function to send all elements of a list to zero bar one

So I am defining a function that maps values to a 1 or 0 depending on whether there is a match or not.

For instance:

    match 3 [1,2,3,4,5] == [0,0,1,0,0]

What I have written so far for defining my 'match' function is:

    let match :: a -> [a] -> [Int]; match x xs = map

And of course I haven't finished writing it out after 'map' and this is what I need help on.

Upvotes: 1

Views: 209

Answers (2)

Lee Duhem
Lee Duhem

Reputation: 15121

Or you could try this some kind of more straightforward version:

match e xs = map (\x -> if x == e then 1 else 0) xs

Upvotes: 1

user2407038
user2407038

Reputation: 14598

match x = map (fromEnum . (==x))

Upvotes: 7

Related Questions