Angel \”Java\” Lopez on Blog

October 9, 2008

F# and Functional Programming Resources

Filed under: .NET, F#, Functional Programming — ajlopez @ 10:13 am

F# is a functional language, created by Microsoft. Implemented under .NET CLR, it’s a typed language, with access to .NET framework. It inherits most of ML/OCaml features.

F# was born in Microsoft Research, and its main creator is Don Syme. Although is a functional language, it supports object programming, too. After years of development, now it’s mature. It’s gaining momentum in scientific community, thanks to its flexibility: it’s not limited to its own library: it can access all .NET framework (The image at left shows F# program running a demo using DirectX from .NET).

Citing Expert F# book promotion at Apress:

While inspired by OCaml, F# isn’t just another functional programming language. Drawing on many of the strengths of both OCaml and .NET, it’s a general–purpose language ideal for real–world development. F# integrates functional, imperative, and object–oriented programming styles so you can flexibly and elegantly solve programming problems, and brings .NET development alive with interactive execution. Whatever your background, you’ll find that F# is easy to learn, fun to use, and extraordinarily powerful. F# will help change the way you think about and go about programming.

There is lot of information about the language. This post is a collection of links, blogs, posts, resources, books, about this very interesting language.

Links

The first page to visit  Microsoft F# Home page:

Microsoft Research’s website for F#
F# Manual
F# Documentation

There are blogs and forums at
hubFS- The place for F# – F# news, forums and blogs

Personal blogs with F# info:
Don Syme’s web log, a key source of information on F#
Robert Pickering’s blog
Tomas Petricek
Granville Barnett’s blog
Luke Hoban’s blog
Chris Smith (F# Tester)
Brian McNamara (F# Dev)
Jomo Fisher (F# Dev)
Andrew Kennedy (MSR)
Luca Bolognese (Managed Languages Principal PM)

Harry Pierson has written many post about functional programming, C# and F#:
DevHawk Functional Programming category

Posts and podcasts

Some links to blog posts (there are hundreds in the above links), only to taste the language and its power:

Episode 18- Matt Podwysocki on F# and Functional Programming Herding Code
Matt Podwysocki puts the fun in functional programming with a deep dive into F#.

Concurrency on a single thread
F# has the async computation expression for writing parallel programs.

F# September 2008 CTP Released
The F# Team has released the F# September 2008 CTP. 

F# Overview (I.) – Introduction Articles TomasP.Net

Functional Understanding

Practical F# Parsing- The Parse Buffer

Play Ball Script in F#

Units of Measure in F#- Part One, Introducing Units

The Weekly Source Code 34 – The Rise of F#

To listen

Herding Code 18- Matthew Podwysocki on F# and Functional Programming
Software Engineering Radio Episode 108 – Simon Peyton Jones on Functional Programming and Haskel
.NET Rocks Episode 310 – Simon Peyton Jones on Functional Programming and Haskell

Examples

F# Samples – Home
Ant Colony Simulation
FsTest
FsUnit

F# Books

  Foundations of F#

Every professional .NET programmer needs to learn about FP, and there’s no better way to do it than by learning F#–and no easier way to learn F# than from Foundations of F#. Written by F# evangelist Rob Pickering, this is an elegant, comprehensive introduction to all aspects of the language and an incisive guide to using F# for real-world professional development.

by Robert Pickering | ISBN-13: 978-1-59059-757-6 | Published May 2007 | 360pp.

 

 Expert F#

Written by F#’s inventor and two major contributors to its development, Expert F# is the authoritative, comprehensive, and in–depth guide to the language and its use. Designed to help others become experts, the first part of the book quickly yet carefully describes the F# language. The second part then carefully shows how to use F# elegantly for a wide variety of practical programming tasks.

by Don Syme, Adam Granicz, Antonio Cisternino | ISBN-13: 978-1-59059-850-4 | Published Dec 2007 | 609pp.

Another book by Jon Harrop: 

F# for Scientists

F# for Scientists will bring you up to speed with basic syntax and programming language concepts. Written in a clear and concise style with practical and enlightening examples, this book is accessible and easy to understand. By reviewing the Visual Studio screen shots that illustrate compilation, debugging and interactive use, you will understand both the functional aspects of F# and the object-oriented task-based features that make F# so useful in practice.

Functional Programming

If you are looking for functional programming in general:

Functional Programming for the Rest of Us

Why Functional Programming Matters a classic by John Hughes

Why Haskell Matters

The classic paper by Backus backus.pdf

A “potpurry” of links:

Are FP and OO Incompatible Syntactic Styles-
A Gentle Introduction to Haskell, Version 98
About Erlang
APL (programming language) (one of my first encounter with FP)
An APL Compiler (Timothy Budd book)
The Cat Programming Language
YouTube – Tangible Functional Programming
Functional Programming Notables #1 (more links to FP)
The Little MLer
functional objects Felleisen
Erlang in Lisp
Free Online Functional Programming Books — FreeTechBooks.com
The Expression Lemma (very geek, category theory, functional programming and LINQ!!)
Chaitin’s construction (more geeky, more category theory, Chaitin visited my country many times, he’s argentinian)
On being stateful
The Glasgow Haskell Compiler
A Neighborhood of Infinity- You Could Have Invented Monads! (And Maybe
InfoQ- Domain Specific Languages in Erlang

Functional C#

.NET and C# are moving to functional style. All you want to know about programming functional C# at Mattew Podwysocki post:

Richmond Code Camp 2008.2 – Functional C# Recap

Other links:

Functional C# Project
Is C# Becoming a Functional Language- – Mads Torgersen
Functional C# – Learn from F# and LINQ

My Delicious

As usual, the links I found useful are added to my delicious. Check:

http://delicious.com/ajlopez/fsharp
http://delicious.com/ajlopez/f%23
http://delicious.com/ajlopez/functionalprogramming
http://delicious.com/ajlopez/haskell
http://delicious.com/ajlopez/erlang

Angel “Java” Lopez
http://www.ajlopez.com/en
http://twitter.com/ajlopez
http://delicious.com/ajlopez

August 13, 2008

Closures in F#

Filed under: .NET, F#, Functional Programming — ajlopez @ 11:30 am

In my previous post about F#:

First steps in F#

I declared that there is no variable in the language, instead, we talk about identifiers. Why? Let’s explore the concept of closure, and how is used in F#.

According to Wikipedia article about closures in computer science:

a closure is a function that is evaluated in an environment containing one or more bound variables

But, what does this mean? To reveal its meaning, let’s assign a value to an identifier, using fsi.exe, the F# interactive interpreter:

MSR F# Interactive, (c) Microsoft Corporation, All Rights Reserved
F# Version 1.9.3.4, compiling for .NET Framework Version v2.0.50727

NOTE:
NOTE: See ‘fsi –help’ for flags
NOTE:
NOTE: Commands: #r <string>; reference (dynamically load) the given DLL.
NOTE: #I <string>; add the given search path for referenced DLLs.

NOTE: #use <string>; accept input from the given file.
NOTE: #load <string> …<string>;
NOTE: load the given file(s) as a compilation unit.
NOTE: #time;; toggle timing on/off.
NOTE: #types;; toggle display of types on/off.
NOTE: #quit;; exit.
NOTE:
NOTE: Visit the F# website at
http://research.microsoft.com/fsharp.
NOTE: Bug reports to fsbugs@microsoft.com. Enjoy!

> let x = 2;;

val x : int

Now, we have an identifier named x. We can show its value:

> x;;
val it : int = 2

A simple integer. Now, we cannot change its value: this identifier was carved in stone as an integer 2. Nothing in the human history could change its value. Is it true? Let’s define a function:

> let dup n = n * x;;

val dup : int -> int

This function dup takes an argument n and multiplies it by the value of identifier x. This identifier is a free “variable” in the function. During the evaluation of the function, x takes its value from the environment. Let’s try now:

> dup 3;;
val it : int = 6

The result was six, as expected. But now, let’s “change” the value of x:

> let x = 3;;

val x : int

> x;;
val it : int = 3

But this is the point to understand closures: this “x” is ANOTHER identifier, that it shadows the previous one. BUT, for the function dup, the identifier x is STILL the previous one. Let’s try again to invoke the function:

> dup 3;;
val it : int = 6

The dup still uses the original x!! That’s the closure in action. If we try:

> dup;;
val it : (int -> int) = <fun:clo@0>

Note the clo @ 0 : its the signature of the closure this function is using. It points to the original environment where the function was defined.

This feature implies that the behaviour of a function doesn’t change after its definition. Even the apparently “free variables” are bounded at definition time. All this is part of the solid fundations of functional programming, in general, and F#, in particular.

About closures and related concept, lambdas, these are the seminal papers:

The Original ‘Lambda Papers’ by Guy Steele and Gerald Sussman

You can learn a lot about Lisp, and closures/lambdas, visiting:

Closures + Lambda = The key to OOP in Lisp « Learning Lisp

 

I implemented some lambdas and closures, in my Lisp interpreter:

AjLisp: a Lisp interpreter in .NET

More C# related stuff about closures:

What’s In A Closure?
Fibonacci Numbers, Caching and Closures

Angel “Java” Lopez
http://www.ajlopez.com/en

August 11, 2008

First steps in F#

Filed under: .NET, F#, Functional Programming, Programming Languages — ajlopez @ 12:22 pm

The F# language has been under development in Microsoft Research. Its main creator is Don Syme. You can visit the F# web site:

http://research.microsoft.com/fsharp/fsharp.aspx

A comment from that site:

Combining the efficiency, scripting, strong typing and productivity of ML with the stability, libraries, cross-language working and tools of .NET.

F# is a programming language that provides the much sought-after combination of type safety, performance and scripting, with all the advantages of running on a high-quality, well-supported modern runtime system.

The last published version, up today, is:

F# 1.9.4.19 realease announcement

Installing F#

You can download a .msi file, or a .zip one. The .msi is an installer program, that install F# compiler, and F# for Visual Studio. I installed the 1.9.3.4 version in my machine. This is one of the screen from my past year installation:

 

 

The last step could take many minutes. It install the support of F# inside Visual Studio. You can use VS or, alternative, a F# command line compiler and interpreter. The F# programs can be launched from Mono, then you can write F# code under Linux.

At the end of install, a folder is created c:\Program Files\FSharp-<version> (it has a .sh file to install the tools in Mono).

There is a new program menu:

The fsi interpreter

We can use an interactive tool, that is the F# interpreter at bin\fsi.exe:

C:\Program Files\FSharp-1.9.3.4\bin>fsi MSR F# Interactive, (c) Microsoft Corporation, All Rights Reserved F# Version 1.9.3.4, compiling for .NET Framework Version v2.0.50727 NOTE: NOTE: See 'fsi --help' for flags NOTE: NOTE: Commands: #r <string>;; reference (dynamically load) the given DLL. NOTE: #I <string>;; add the given search path for referenced DLLs. NOTE: #use <string>;; accept input from the given file. NOTE: #load <string> ...<string>;; NOTE: load the given file(s) as a compilation unit. NOTE: #time;; toggle timing on/off. NOTE: #types;; toggle display of types on/off. NOTE: #quit;; exit. NOTE: NOTE: Visit the F# website at http://research.microsoft.com/fsharp. NOTE: Bug reports to fsbugs@microsoft.com. Enjoy!

Using let

Let’s enter

> let x = 10;;

We get at output:

val x : int

The ;; makes the program to compile and execute the entered commands. In this command, x is an identifier, that has an integer value. F# is a typed language. We didn’t define the type of the identifier x: it was inferred automatically from the value we put in it. Let’s note that is an identifier, not a variable. Once an identifier is set, we can’t change its value. That is a distinctive feature of functional programming in general. I’ll explore that behavior in future posts.

Now, let’s enter

> x;;

(the > was added by the program, it’s the fsi’s prompt) We get

val it : int = 10

The fsi program prints the expression and its type. it is the name that it uses when we don’t speficy a target identifier.

Defining functions

Using the same let, we can define functions:

>  let dup x = x * 2;;
val dup : int -> int

For F#, values and functions are the same. A function is a first class citizen in the F# language, and it can be manipulated as any other value.

The second line above shows that dup is a function that receives an integer and returns another integer. The identifier dup now points to that function. It is its value.

We can apply the function entering:

> dup 10;;
val it : int = 20

A function is a value, we can pass a function as a parameter:

> let apply f x = f (f x);;
val apply : (‘a -> ‘a) -> ‘a -> ‘a

The result is something more cryptic. The term ‘a refers to a undefined type. It could be an integer, or an string, or something else. But if we give an integer as second parameter to apply, the first one MUST BE a function that takes an integer and returns another integer. Don Syme, the creator of F#, was involved in the definition and implementation of generics in .NET 2.x. One could think he implemented that features to create the F# language… ;-)

If we entered

> apply dup 2;;

then we get

val it : int = 8

that is the same that

> dup (dup 2);;

Conclusion

For those who are not familiar with functional programming, the concept of a function as a first class value, citizen with the same rights and duties of a value, could sound something strange. But if you can grasp the base of this approach, you’ll appreciate the advantages of this path (the appearance of lambda and delegates in mainstream .NET is an example of such adoption). If you have seen a Lisp or other functional implementation, all this sounds as a deja vu for you.

The interesting part of F# is that not only supports the functional programming paradigm but also interacts with the rest of the .NET framework. Then, F# is not an “academic” language, only related to itself, but was adopted in many projects. Although I was dealing with the command line interpreter in this post, we can use the Visual Studio, leveraging debugging, projects, and editors.

Enjoy F#!

Angel “Java” Lopez
http://www.ajlopez.com/en

Theme: Shocking Blue Green. Get a free blog at WordPress.com

Follow

Get every new post delivered to your Inbox.

Join 67 other followers