The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Typing like a Functional Programmer
by Christopher Diggins
November 26, 2006
For a long while I didn't fully grasp the funky syntax of typed functional languages like ML and Haskell, until I realized a fundamental difference between many typed functional languages and common imperative languages.


This purpose of this blog entry is to help programmers more familiar with imperative languages like C++, C# and Java to make the transition to programming in a typed functional language like OCamL, Haskell, or F#.

An imperative programmer would normally think of integer addition as being a function which takes two integers and produces a new integer. You could write this concept as follows:

  add_ints : (int, int) -> (int)
Now ask yourself what happens if you pass only one integer to an integer addition function? In most imperative languages it simply fails to compile, but in functional land the answer is: you get a new function.

This new function takes a single integer and returns a single integer. Using the earlier type notation this alternative view of integer addition could be written as:
   add_ints : (int) -> ((int)->(int))
This is a key concept in functional languages called currying. An integer addition function either returns an integer if it gets two integers, or it returns a function which takes a single integer and returns an integer.

When you think about it this way, you may realize that the first way of understanding add_ints can be replaced by the second. Consider:

  add_ints(x, y) = add_ints(x)(y)
This approach can be generalized if a language enforces the rule that all functions of more than one parameter are functions which take one parameter and return another function.

With that generalization then you might realize the parantheses are unneccessary in both calling the function and expressing the type. For example:

   add_ints : int -> int -> int
   add_ints x y       
This is now almost exactly what you would write in a typed functional language like ML, OCaml, or Haskell.

This might take some time to digest (it did for me, but I'm not really that bright), but hopefully it might help illuminate your way through the tangled woods of typed functional programming.

Talk Back!

Have an opinion? Readers have already posted 51 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at

This weblog entry is Copyright © 2006 Christopher Diggins. All rights reserved.

Sponsored Links


Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use