Geeks With Blogs



Add to Technorati Favorites

An Archived Managed World This blog has moved to

As I’ve been writing more F# code and learning more about the F# language, I have found myself intrigued by how some surprisingly simple language features can combine with each other to enable us to write very expressive code. While I am still learning F# (and realize just how much more I have to learn), I’m hoping that sharing some of these thoughts will encourage others to dig into what I think is a very powerful language that a lot of us developers can learn from.

In this post, we’re going to talk about Infix Operators. Before we dive into F#’s implementation of infix operators though, let’s discuss what an “Infix” Operator actually is.

So what is an “Infix” operator? An infix operator is an operator that is expressed using a mathematical notation called infix notation. When it comes to these types of mathematical notations, there are three primary kinds: Prefix Notation, Infix Notation, and Postfix Notation.

Take for example the addition operator. The addition operator takes two operands that are then added together. Using infix notation, the operator is written between the two operands it operates upon. Prefix notation would have the operator before the operands, and postfix notation has the operator after the operands. So given the addition operator, the different notations would like the following:

  • Prefix notation: + 3 4 (definitely familiar to LISP-heads –> “(+ 3 4)”)
  • Infix notation: 3 + 4 (familiar to most of us developers)
  • Postfix notation: 3 4 + (an interest way used by Forth for one, a stack-as-first-class-citizen language –> “3 4 ADD”)

One of the things I love about F# is that infix operators are normal functions that you can easily define yourself. Let’s explore this concept a bit. Open up F# Interactive and execute the following command:

> (+);;
val it : (int -> int -> int) = <fun:clo@42>

Note: Surrounding the infix operator with “()” allows us to get at the underlying function that implements the infix operator, rather than the operator itself.

Heck, we can even call it like a normal function:

> (+) 3 4;;
val it : int = 7

You can see here that the addition operator is simply a function that accepts two integers and returns an integer (well, this isn’t technically true, but you can think about it this way for now; we’ll get into the difference in a different post). I said before we can easily define our own infix operator. How do we do that? Well, just for giggles, let’s go ahead and define our own add operator:

> let (|+) x y = x + y;;
val (|+) : int -> int -> int

After we define this function, we can use our new infix operator just like any of the other built-in infix operator:

> 3 |+ 4;;
val it : int = 7

Yes, it’s that simple.

Well, that’s a very quick look at a seemingly simple language feature that a lot of us developers tend to take for granted. In future posts, we’ll see how combining infix operators with other features in F# allows us to write more expressive F# code.

Posted on Friday, April 17, 2009 10:03 PM Functional Programming , Development | Back to top

Comments on this post: F# Basics – Infix Operators

# re: F# Basics – Infix Operators
Requesting Gravatar...
FYI, you can define symbolic functions in F# to use the 'infix' notation by prefixing them with a tilde:

> let (~++) x y = x + y;;

val ( ~++ ) : int -> int -> int

> ~++ 4 6;;
val it : int = 10

Nice post!
Left by Chris Smith on Apr 17, 2009 10:33 PM

# re: F# Basics – Infix Operators
Requesting Gravatar...
Hey Chris, just to clarify, you mean "prefix" instead of "infix", right? That's cool I didn't know of that one.

I wonder though, if applying to more than one operand, why not just declare a regular function rather than a prefix operator?
Left by Jason Olson on Apr 18, 2009 10:21 AM

# re: F# Basics – Infix Operators
Requesting Gravatar...
I know some programming languages like C, C++, C#,, ASP.NET but never heard F# before. Make me wonder what kind of programming is F# and certainly I will find some more information about this F#.
Left by cheap dedicated hosting on Apr 20, 2010 1:43 PM

# re: F# Basics – Infix Operators
Requesting Gravatar...
Well, it is seems that another junk programming language is new to many and that s the only way for Microsoft to stay in Bussiness
Left by Ironben on Aug 30, 2010 9:59 AM

# re: F# Basics – Infix Operators
Requesting Gravatar...
This is kind of off-topic, but why does Microsoft think it can compete with Apple? They may have the oldies, but the younger gens are Macs all the way.
Left by TrailersPlus on Nov 03, 2010 2:25 AM

# re: F# Basics – Infix Operators
Requesting Gravatar...
Left by Raymond Ogburn on Mar 22, 2011 7:06 PM

Your comment:
 (will show your gravatar)

Copyright © Jason Olson | Powered by: