Home > Software design >  Haskell: What does the : operator mean in an anonymous function within map?
Haskell: What does the : operator mean in an anonymous function within map?

Time:11-27

I got trouble to understand this line:

combs val = [] : concatMap (\w -> map (:w) val) (combs val)

The purpose is to create all combinations possible. It is an infinite list. (combs :: [a] -> [[a]])

I don't understand the anonymous function: \w -> map (:w) val

What does (:w) mean? What kind of mapping function is that? In my perception \w must be a list? Is it a concatination? I am confused.

CodePudding user response:

(:w) is a section. It is equivalent to

\x -> x:w

This holds for every infix operator. We have

(  w) = (\x -> x w)
(* w) = (\x -> x*w)
(/ w) = (\x -> x/w)

and so on. (Only exception: (-x) is unary minus, so it is a number and not a function)

This also applies to sections where the infix operator is on the other side:

(w/) = (\x -> w/x)

In your specific case, map (:w) prepends each element of the input list to the list w, e.g.

map (:w) [a,b,c] = [a:w, b:w, c:w]
  • Related