Home > Enterprise >  How to use map instead of list comprehension (Haskell)
How to use map instead of list comprehension (Haskell)

Time:10-18

I have made this code which replaces all elements from a list which fulfilss a predicate:

gRep :: (t -> Bool) -> t -> [t] -> [t]
gRep pred x ys = [if pred y then x else y | y <- ys]

so f.eks I get these outputs:

ghci> gRep (<'g') 'z' "abcdefghijklmnopqrstuvwxyz"
"zzzzzzghijklmnopqrstuvwxyz"
ghci> gRep (<3) 0 [1,2,3,4,5,6,7,8,9]
[0,0,3,4,5,6,7,8,9]

How can I write this function using map instead?

I tried to write it like this:

gRepMap :: (t -> Bool) -> t -> [t] -> [t]
gRepMap pred x ys = map (\zs -> if (pred y) then x else y | y <- ys) ys

But I get a parser-error because of | y <- ys . I know this may be a possible duplicate, but I couldnt find a question which dealt with this particular conversion.

CodePudding user response:

The variable in the lamba expession \zs -> … is zs this variable will ues the elements in the list, since the lambda expression is called with each element of th elist, as a result you thus perform the mapping with:

gRepMap :: (t -> Bool) -> t -> [t] -> [t]
gRepMap pred x ys = map (\zs -> if pred zs then x else zs) ys

Usually in Haskell a variable name ends with an s to denote a collection of items (especially when working with lists). Here zs is an element of that list, it thus makes more sense to name the variable y or z, not zs:

gRepMap :: (t -> Bool) -> t -> [t] -> [t]
gRepMap pred x ys = map (\y -> if pred y then x else y) ys
  • Related