F# lets you turn operators into functions by surrounding them with (
)
: for instance, (+)
is of type int -> int -> int
.
Is it possible to do this with the list cons operator, ::
?
It doesn't behave like a normal binary operator:
FSI> (::);;
(::);;
-^^
c:\temp\stdin(3,2): error FS0010: Unexpected symbol '::' in expression.
Expected ')' or other token.
And the List.Cons
method takes a tuple; it's not curried.
(It's useful to be able to do this. For instance, you can use it to implement map in terms of fold).
Separates the elements of a tuple, or type parameters. :: Lists. Match Expressions. Creates a list.
F# Lists Creating lists A way to create a list is to place elements in two square brackets, separated by semicolons. The elements must have the same type. It is also possible to define lists of functions, of elements of a type defined previously, of objects of a class, etc.
In Scheme, car , cdr , and cons are the most important functions. The cons function is used to construct pairs and pairs are used to construct the lists. The car and cdr are used to access data and return accordingly first and second element from a pair.
Cons is a historic name, though, originating from Lisp. :-: is another arbitrary name for the constructor, except that it can be used infix. I.e. instead of Cons 1 someList one can write 1 :-: someList .
In Scala, List has an operator ::, which is known as the Cons operator. It is useful to add new elements at the beginning of the List. Here, Cons is short for construct the new List object. Let us explore the Cons operator with some simple examples (here :: is a double colon):
These operators are used to perform logical “AND”, “OR” and “NOT” operation, i.e. the function similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consideration.
There are two built-in operators on lists. The :: or cons operator, adds one element to the front of a list. The @ or append operator combines two lists: We can write functions which operate over lists by pattern matching: This function operates not just on lists of integers, but on any kind of list. Why is this?
in and not in are the membership operators; used to test whether a value or variable is in a sequence. Precedence and Associativity of Operators: Operator precedence and associativity determine the priorities of the operator.
Paraphrased from http://cs.hubfs.net/forums/permalink/11713/11713/ShowThread.aspx#11713
(::)
is a discriminated union 'constructor' for the list<'a> type
, and so raised the question of whether as a function value its arguments should be curried (like +
) or tupled (like all DU constructors). Either way seems fishy/unexpected to some people, so F# simply disallows the construct.
Of course you can always write e.g.
let cons x y = x :: y
and use cons
, or just use a lambda fun x y -> x::y
, if you want a "curried prefix function of two args" for this.
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