Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use list cons operator (a :: b) as a function

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).

like image 842
Tim Robinson Avatar asked Sep 29 '10 12:09

Tim Robinson


People also ask

What is :: in F#?

Separates the elements of a tuple, or type parameters. :: Lists. Match Expressions. Creates a list.

How to create a list in f#?

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.

What does the function cons in scheme do?

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.

What is cons in haskell?

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 .

What is the cons operator in Scala?

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):

What is the use of and and or operators in C++?

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.

What are the built-in operators on lists?

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?

What is the use of in and not in operators?

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.


1 Answers

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.

like image 172
Brian Avatar answered Nov 16 '22 02:11

Brian