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?
Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands—"infixed operators"—such as the plus sign in 2 + 2.
User defined infix function notation –It must be member function or extension function. It must accepts a single parameter. The parameter must not accept variable number of arguments and must have no default value. It must be marked with infix keyword.
Haskell provides special syntax to support infix notation. An operator is a function that can be applied using infix syntax (Section 3.4), or partially applied using a section (Section 3.5).
Infix notation: X + Y. Operators are written in-between their operands. This is the usual way we write expressions. An expression such as A * ( B + C ) / D is usually taken to mean something like: "First add B and C together, then multiply the result by A, then divide by D to give the final answer."
:
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]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With