What does (:) do in Haskell?

haskell

Solution

While the other answers correctly explain what `:` is they don't quite answer the question - in the answer you have in your question `:` isn't used as a function, but as a constructor to pattern match on. `fun (x:xs) = x` means "if the argument is of the format (x:xs) give me the x". Pattern matching is used to "pull apart" complex types based on their constructors in Haskell.

In particular, since `:` is a list constructor you can pull apart lists with `:`

(conceptually list is defined as `data [] a = [] | (:) a [a]`, although you're not gonna get this to compile because it's builtin syntax).

A non list example: We could define a datatype `data F a b = A a | B b`. This would create a type `F` that's parameterized with two types `a` and `b` and two constructors `A` and `B` with the types `a -> F a b` and `b -> F a b` respectively.

You could then write functions that use pattern matching to get at the contained values, like

isA (A _) = True -- this value was constructed with A, so it is an A
isA (B _) = False -- this value was constructed with B so it is not an A

or

getA (A a) = a -- this value was constructed with A so we can get an a out of it
getA (B _) = undefined -- ohps! We can't get an a back here cause we don't have one!

Problem

I've tried looking it up in hoogle and other various haskell dictionaries, but I can't find it. I was under the impression that it prepends, but I'm starting to see it in ways I haven't before and I've started second guessing myself. For example, this is one of the questions that I don't understand: (3 points) Fill in the blank with a pattern such that fun1 [(5,6),(7,8)] returns 5 and fun1 [(10,20),(30,40),(50,60)] returns 10: and the answer is apparently: ``` ((y,_):_) fun1 _____________ = y ``` But I am so confused by this. I understand that the underscores mean that you don't really care about what the types of those are, but I don't understand what the (:) does in this answer.

Original source

Related problems