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

Viewed 3175

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

3 Answers
Related