58

I'm reading A Gentle Introduction to Haskell (which is not so gentle) and it repeatedly uses the : operator without directly explaining what it does.

So, what exactly does it do?

1
  • 26
    It's not gentle at all. If this is your first contact with functional programming I recomend this site: learnyouahaskell.com After reading through this site then return to A Gentle Introduction. It really smooths the way a lot. Commented Nov 10, 2009 at 0:52

4 Answers 4

91

: is the “prepend” operator:

x : xs

Returns a list which has x as first element, followed by all elements in xs. In other functional languages, this is usually called cons, because it “cons”tructs a list recursively by repeated application from an empty list:

1 : 2 : 3 : 4 : []

is the list [1, 2, 3, 4].

5
  • 3
    how is this different than the ++ operator which concatenates lists?
    – dlkulp
    Commented Apr 10, 2017 at 5:13
  • 8
    @dlkulp ++ concatenates one list to another. With : the first argument is of type a (an arbitrary type) and the second argument is of type [a] which is the type of lists of elements of a. Commented Sep 27, 2017 at 4:37
  • Actually, "cons" comes from LISP - "In computer programming, cons (/ˈkɒnz/ or /ˈkɒns/) is a fundamental function in most dialects of the Lisp programming language. cons constructs memory objects which hold two values or pointers to values. These objects are referred to as (cons) cells, conses, non-atomic s-expressions ("NATSes"), or (cons) pairs." Commented Jun 9, 2021 at 22:06
  • 1
    @ChaosRules Yes that’s correct, but my answer didn’t say anything differently. Commented Jun 10, 2021 at 7:29
  • "cons"-tructor! i'm getting old, man. i couldn't quite figure out "why is the nickname 'cons'?"
    – 6cef
    Commented Sep 8, 2023 at 17:37
25

Could always check out the types in GHCi/HUGS, as the first steps in the tutorial encourage you to download GHC/HUGS.

Prelude> :t (:)
(:) :: a -> [a] -> [a]
Prelude> :t (++)
(++) :: [a] -> [a] -> [a]

From their respective types, it's quite easy to deduce their usage.

PS: http://haskell.org/hoogle/ is awesome.

4
  • 1
    Why thank you sir! If you download hoogle, you can use it as a function within ghc. It's pretty sweet. Allows you to search by type or function name without having to import, beats :t in every way.
    – codebliss
    Commented Nov 10, 2009 at 0:29
  • Technically (:) could be x (:) xs = xs or x (:) xs = xs ++ [x] Commented Jul 15, 2011 at 21:12
  • 3
    I find :i to (sometimes) be a lot more informative than :t. :i (:) will give you data [] a = ... | a : [a], and :i [] will give you the complete list definition, data [] a = [] | a : [a]. Commented Mar 20, 2012 at 18:24
  • 3
    You can't find (:) on hoogle. Why not?
    – raine
    Commented Oct 17, 2014 at 9:42
17

The : operator in Haskell is the constructor for lists. It 'cons' whatever is before the colon onto the list specified after it.

For instance, a list of integers is made by 'consing' each number onto the empty list, e.g;

The list [1,2,3,4] can be constructed as follows:

  • 4 : [] (consing 4 to the empty list)
  • 3 : [4] (consing 3 onto the list containing 4)
  • 2 : [3,4] (consing 2 onto the list containing 3, 4)
  • 1 : [2,3,4] (consing 1 onto the list containing 2,3,4)

giving you;

[1,2,3,4]

Written fully that's;

1 : 2 : 3 : 4 : []
12

It is the type constructor for lists. It is no different from any other type constructor like Just or Left, except that it is infix. Valid type constructors can either be words starting with a capital letter, or symbols starting with a colon.

So you can define infix constructors for your own data types. For example:

data MyList a = a :> MyList a
              | Empty

in the above code we define a type called MyList with two constructors: the first is a weird-looking constructor :> which takes an element and another MyList a; the second is an empty constructor Empty which is equivalent to [] in Haskell's native lists.

The above is equivalent to :

data MyList a = Cons a  (MyList a)
              | Empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.