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.