got net?

Kevin Hazzard's Brain Spigot

About the author

Welcome to Kevin Hazzard's blog.
E-mail me Send mail

Recent posts

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Exploring the F# Language Series Part 1 - What is F#?

Throughout this series, I will be exploring the F# (pronounced F Sharp) language as a beginner. Perhaps you're just like me in that you've never worked with the F# language before but you are very curious about it. You may not understand the hype you've been hearing about so-called functional languages. But that's OK. If you want to learn along with me, that would be great.

Along the way, I welcome your comments and feedback, both to instruct me and other readers. You can get an overview of the complete series by visiting the series index. Enjoy.

 

Part 1 - What is F#? 

If you are like me, a software developer with a C++/C# background, you may have heard about the F# language but you haven't had a chance to learn anything about it yet. You may also be a Java developer or a Visual Basic developer who's curious. Don't let the name fool you. You don't have to be a C# developer to love F# once you start learning about it. As we'll see, the F# language doesn't have strong roots or ties to languages like C, C++, Java, C# or Visual Basic. As such, I think it can teach us a lot about alternative ways of thinking.

An FAQ (of Sorts) for Defining the F# Language

Here are some questions I've accumulated about the F# language. My answers below aren't definitive, by any means. But this is what I've learned so far. If you have other questions that you think belong in an entry-level FAQ for the F# language, let me know and I'll add them.

  • With a name like that, is F# a derivative of C#?
  • What does the F in the F# language's name mean?
  • What is a functional programming language?
  • What is an imperative programming language?
  • Is F# object-oriented?
  • Can F# be integrated into Visual Studio?
  • Are there command line tools for working with F#?
  • Is F# a portable language?
  • Does F# require me to deploy a runtime library?
  • Is F# a scripting language or a compiled language (or both)?
  • How does F# perform in terms of speed as compared to other languages?
  • Where can I go to find out more about F#?

With a name like that, is F# a derivative of C#?

Not at all. F# comes from a family of languages that were spawned by a language created in the late 1970s known as ML or MetaLanguage. The ML language has a special type inference algorithm built into it that allows it to infer the types of most expressions automatically. This allows the language to feel dynamically typed like many scripting languages while actually being statically typed. F# behaves this way as well. In F#, you can declare types but you don't often need to do it.

More recently, F# is a derivative of a language known as Objective Caml (or OCaml) which extended Caml (Categorical Abstract Machine Language), an ML derivative, by adding certain object-oriented capabilities to it. F# has a high degree of compatibility with OCaml source code as a result.

What does the F in the F# language's name mean?

I'm sure it's debatable but the F in F# mostly likely stands for the term Functional. It's hard to get an exact answer on this one but one can assume that since F# is often touted as a functional language, that must be the meaning. F# is not just a functional language, however. It's a multi-paradigm language that allows for functional programming as well as imperative programming. So, it's conceivable that the language could have been called I#. But the functional feature of F# sort of steal the show so, the name makes sense.

What is a functional programming language?

Words are designed to conjure up ideas and images. So, what does the word functional mean in the context of a programming language. Merriam-Webster's dictionary defines functional as:

1 a: of, connected with, or being a function b: affecting physiological or psychological functions but not organic structure <functional heart disease>
2: used to contribute to the development or maintenance of a larger whole <functional and practical school courses>; also : designed or developed chiefly from the point of view of use <functional clothing>
3: performing or able to perform a regular function

As an American English speaker, the third definition is the one that I'm likely to think of when I hear the word functional. If something is functional, it's performing as it is supposed to. For other English dialects, the term functional could conjure up other meanings. But when I hear the term functional programming language, I'm likely to think of a programming language that's able to do what it was designed to do.

Of course, that makes no sense at all. If programming languages didn't perform as we expected them to, we wouldn't call them programming languages at all. We would call them probability languages or gee-i-sure-hope-this-thing-does-something-useful-when-i-press-the-button languages. Instead, the term functional in the realm of programming languages relates to the use of functions (or callable mathematical parts) as the basis for computation rather than changes in state and the mutation of data. So functional programming languages emphasize the second of the three definitions given above, i.e. viewing computation as the sum of the parts (functions) that are used to describe a larger solution.

In the 1930s, something called the Lambda Calculus was conceived to explore how functional decomposition of a problem could be used to more naturally describe potential solutions to it. Nowadays, functional programming languages are just implementations of the Lambda Calculus as a specific system with some extra bells and whistles, as they say. As functional programming languages go, however, F# is not a pure because it includes constructs for mutability, i.e. changing the state of objects in certain cases.

What is an imperative programming language?

Just as we did for the term functional above, it's probably worth investigating what the word imperative means at this point. Merriam-Webster defines imperative this way:

1 a: of, relating to, or constituting the grammatical mood that expresses the will to influence the behavior of another b: expressive of a command, entreaty, or exhortation c: having power to restrain, control, and direct
2: not to be avoided or evaded : necessary <an imperative duty>

As an American English speaker, both definitions come to mind. Imperatives are about the will and the duty to change things. The term imperative programming language is understandable in this context because imperative programming is all about state management.

To understand how imperative systems are built, think about the modern computer system. From the hardware up, all computers are somewhat imperative in nature. We store memory as a series of electrical signals and manipulate those signals to change the meaning of the data they represent. Registers on the CPUs change value as programs execute to track things like the position of a stack pointer or the location of the next executable instruction in memory. If you think about it, the entire computer system is nothing more than a very large state machine with a googolplex of possible states.

Imperative systems are, by definition, mutable, meaning that they can mutate or change in some way over time to create state that is representative of the progress of the computation being performed. Imperative programming languages, in particular, usually allow the programmer to define global and/or local state through various memory constructs like variables and classes. Because F# is based on OCaml, it has some object-oriented features that depend on mutable data. As we'll see later, the F# keyword mutable is at the heart of the language's imperative programming model.

Is F# object-oriented?

Yes. F# supports the .NET typing model including:

  • Classes
  • Inheritence
  • Interface implementation
  • Polymorphism

All types in F# ultimately derive from the .NET type System.Object so the model is completely unified and compatible with other .NET languages.

Can F# be integrated into Visual Studio?

The installation package for F# includes complete integration with Visual Studio 2003, 2005 and 2008. Included are:

  • A new F# project type (with file extension .fsharpp)
  • Templates for F# source, scripts and interfaces (including ML-compliant ones)
  • Templates for F# Lex and Yacc source
  • Integrated debugger support
  • A tool window for using the command-line tool called FSI.EXE (F# Interactive) for testing and running F# scripts and code

Are there command line tools for working with F#?

Yes. In fact, many people (like me) prefer working with the F# command line tools most of the time. There are F# command line tools in the installer package for:

  • F# compilation (FSC.EXE)
  • F# interactive interpreter (FSI.EXE)
  • F# Lex compiler (FSLEX.EXE)
  • F# Yacc compiler (FSYACC.EXE)
  • Resource compiler (RESXC.EXE)

Is F# a portable language?

Using the #light directive, F# can compile or interpret most OCaml code. So if you use that directive, your F# code should port back to OCaml without much effort. However, dependence on .NET types that are not available on other platforms will, of course, make your F# code non-portable (or at least portable with a lot of effort to replace the missing types).

Does F# require me to deploy a runtime library?

Yes. There is an assembly called FSharp.Core.dll which is referenced by your compiled F# code. A command line flag for the FSC.EXE compiler exists called --standalone. If you use this flag, the compiler will statically link the core components in so that you don't have to deploy the core assembly with your compiled F# assemblies. Using the --standalone flag will add between one and two megabytes to your F# assemblies, though.

Is F# a scripting language or a compiled language (or both)?

F# is definitely a compiled language. But it also supports  scripting via FSX files. You can run FSX scripts at the command line using the --exec flag of the F# Interactive (FSI.EXE) tool. This is useful when you want to run some F# code without having to compile it first.

How does F# perform in terms of speed as compared to other languages?

Compiled F# runs about as fast as C# or C++. I don't have personal benchmarks yet but empirically and anecdotally, compiled F# seems to be about as fast as other compiled .NET languages. The F# compiler has a cross-module optimizer that can be enabled using the -O command line flag. This flag is turned off by default. Of course, without hard performance data, my assessment remains quite subjective. We will examine F# performance in detail later on in the series.

Where can I go to find out more about F#?

Here are some links I've found useful:

That's all for now. Feel free to take a look at the other parts of this series exploring the F# language by visiting the series index.


Categories: F# | Software Dev | Series
Posted by kevin on Sunday, August 03, 2008 12:00 PM
Permalink | Comments (14) | Post RSSRSS comment feed

Comments

zj People's Republic of China

Monday, August 04, 2008 5:09 AM

zj

you strong.Why did not the Chinese national dropdownlist optional.

Kevin Hazzard, MVP United States

Monday, August 04, 2008 7:09 AM

Kevin Hazzard, MVP

@zj, that's a good question. I am using the BlogEngine software which didn't include China in the country dropdownlist for some reason. I'll check into adding it.

Kevin Hazzard, MVP United States

Monday, August 04, 2008 9:51 AM

Kevin Hazzard, MVP

@zj, The full name of your country in English is "People's Republic of China". It's already in the dropdownlist. I repaired your comment above. See? Take care.

Andrew Wagner United States

Monday, August 04, 2008 1:23 PM

Andrew Wagner

Interesting comment about the F in F# standing for 'Functional'. I thought it came from 'System F', the type system.

Brian United States

Monday, August 04, 2008 3:32 PM

Brian

Cool intro article!  One minor note:

Using the #light directive, F# can compile or interpret most OCaml code.

In fact if you want to cross-compile with OCaml, you should not use #light.  See also
research.microsoft.com/.../ml-compat.aspx

Jon Harrop United Kingdom

Monday, August 04, 2008 4:08 PM

Jon Harrop

Great article!

I will knit pick on one point though: although F# and OCaml do share a common subset it is not very big and, consequently, very few OCaml programs will run in F# unaltered. Moreover, you said "with #light" when, in fact, the #light syntax option actually damages OCaml compatibility. For example, the following OCaml idioms do not work with #light:

  let f x y = match x, y with
    | _ -> true

  let x=2 and y=3 in

More specifically, labelled and optional arguments are present in F# in a very limited form and using a completely different syntax, comprehensions are different in F#, quotations are completely different in F#, functors do not exist in F#, polymorphic variants do not exist in F#, structural types do not exist in F#, co- and contra-variance annotations are not supported in F#, abstract types are not fully supported in F# (e.g. you cannot abstract a type abbreviation) and regular expressions are not the same in F#.

The APIs are also largely different so any XML, Web, concurrent, GUI or graphical applications will be completely unportable. Moreover, all of the main OCaml libraries (e.g. LablGTK, LablGL) make heavy use of many of these features that are not found in F# so it is not even possible to mimic the functionality with a shim. There are also major differences between the tool sets, most notable Camlp4 does not exist in F# and fslex does not implement named regexps (which breaks almost all of my lexers and is very tedious to work around).

Indeed, there are so many fundamental differences between the languages that even my trivial 19 line mini Sudoku solver requires substantial changes to work in F#.

The one thing that I would say is that F# is sufficiently similar to OCaml that OCaml programmers will be able to pick it up very quickly, not least because the non-trivial aspects of OCaml are regarded as research prototypes and, consequently, are not implemented in F#.

Cheers,
Jon.

Kevin Hazzard, MVP People's Republic of China

Monday, August 04, 2008 11:04 PM

Kevin Hazzard, MVP

@brian @jon Thanks. That's what I'm looking for. Community provided content at its best.

Kevin Hazzard, MVP People's Republic of China

Monday, August 04, 2008 11:05 PM

Kevin Hazzard, MVP

@andrew That could be. It's just as good a theory as mine. If you have definitive proof of the origins of the name F#, please let me know and I will update the article. Thanks.

Kevin Hazzard, MVP United States

Monday, August 04, 2008 11:06 PM

Kevin Hazzard, MVP

Oops. I was playing with country settings on comments earlier today and forgot to switch from China. Back in the US now! Smile

somard United States

Tuesday, August 05, 2008 1:26 PM

somard

Very interesting introduction. The flow is smooth and layout is crystal clear. Two thumbs up. I waiting for the next installment.

As a sideline: on one of the Dr. Dobb's magazines, there was an article, touting small bee, another functional language, geared towards math. I have not any much reading on that topic. But I find it is illuminating that there are so much good stuff out there, besides c++, a language I have been breathing in the past 10 years or so.

Kevin Hazzard, MVP United States

Tuesday, August 05, 2008 1:45 PM

Kevin Hazzard, MVP

@somard I am working on the third installment in this series now. I can only work on it in the evenings so it will most likely be ready on Friday morning, 8 July 2008. Thank you for the feedback. I, too, am happy that so many interesting languages are flourishing in the Microsoft world. The .NET Framework and the Framework Class Library are so rich. To be able to express ideas in so many languages on top of that framework is just great. Take care.

zj People's Republic of China

Monday, August 11, 2008 1:52 AM

zj

Thank you, you are a good man!

Zehra Nasif United States

Tuesday, August 12, 2008 2:34 PM

Zehra Nasif

I always enjoy reading articles which are written with easy to read and good content. This was a great introduction to F# and looking forward to read the next installments. When I first heard about F# a year ago, I thought that was a bad name for a new language if it does not offer more than existing programming languages. I am curious what F# can do and others can't.

reza Iran

Sunday, October 26, 2008 6:28 AM

reza

mersi

Comments are closed