September 8, 2009

Haskell

Recently I hake taken a break from F# and tried to learn Haskell which is one of the language inspired F#. I would like to educate myself more on the fundamental concepts in functional programming before I go further into F#. Haskell seems to be simpler than F# and has been developed for near twenty years. It is officially available in various platform including Windows, Mac OS X and Linux. Haskell platform is the starting point.

November 6, 2008

Basic Syntax

This article will cover some very basic syntax elements to jump start the learning of F#. It is not intended to provide a full explanation of each construct so as to make it short.

Assignment

let x = 10

let y = if x> 10 then 1 else 2

let mutable z = 1
z <- z + 1

Notes:
1. Value of x cannot be changed. So we refer x as a
value instead of a variable.
2. z is mutable
3. Use <- operator to change the value of a mutable value
4. Specifying a type is not necessary as the type will be inferred by the RHS value.

Function Declaration

let f n = 10 * n + 2

let f a b = a * 2 + b

Notes:
1. No parenthesis is surrounding the argument
2. No comma to separate arguments


Calling a function

let x = f 10
let y = f 10 + 1
let z = f (x + 10)

Notes:
1. No parenthesis is surrounding the argument
2. y = x +1 as function calling has a higher precedence than binary operators
3. Parenthesis is used to change the association

Recursion

let
rec f n = if n > 0 then (f n-1) * 2 else 1

Notes:
1. rec prefix must be used to allow a function to call itself.
2. Recursive function must have a terminal case (e.g. n = 0 -> 1).

Looping

for i = 1 to 10 do printfn i

for i = 1 to 10 do
printf "Hello,"
printfn "World"

Notes:
1.
printf and printfn are built-in functions for printing to console. printfn terminates the line with a carriage return
2. Indentation is significant to define the boundary of the block (so no
next statement is required)


Conditional

let f n = if n > 10 then
let m = 20
let p = m + n
n
else
n - 1

Notes:
1. Indentation is used to define the block
2. Last expression within a block will be used as the return value
3. If the else part is not defined, compilation error will be resulted

This concludes a general introduction to the basic syntax of F#.

October 28, 2008

What is F#

"A succinct, type-inferred, expressive, efficient functional and object-oriented language for the .NET platform."
Microsoft Research


F# is a succinct, type-inferred functional programming language for developed by Microsoft Research by Don Syme and James Margetson.

F# is a multi-paradigm programming language as it also supports object-oriented programming.

F# has its heritage from ML family especially OCaml. It also borrows features from C# and Haskell. F# is not a pure functional language as it allows side-effect and mutable variables.

F# is highly compatible with OCaml (may require some minor conditional compilation).

F# is the first class citizen in .NET platform which means F# compiler generates MSIL just like other languages in .NET platform. F# can access modules written in other .NET languages and verse versa.

In future release of Microsoft Visual Studio, F# will be included.

In the current stage, F# can be downloaded as a separated package with VS2005 and VS2008 integration provided. It may be possible to have F# Express, just like VB Express and C# Express, for hobbyists.

F# can also be used in Linux and Mac OSX via Mono 2.0 Framework.

Currently no known non-Microsoft version of F# exist.




October 12, 2008

Table of Contents

In order to allow the contents of this blog to be accessed in an organized manner, I have laid out a plan for writing the blog. This list will continue to grow along with the time and my understanding in F#. I will create new posts just like other blogs but I will then link the posts with this list so that audience can read the contents of this blog like a book. If you have suggestions in the list, feel free to leave a comment.


Preface
A Voyage to Functional Programming in F#

Introduction
- What is F#
- Why F# Matters

Getting Start with F#
- Installation
- Basic Syntax

Key Concepts
- Basic Types and Inferred Types
- Immunity
- List, Array, Record and Sequence
- Discriminated Union
- Option Type
- Function As Value
- Recursion in F#
- Currying

Advanced Concepts
- Active Pattern
- Concurrency
- Asynchronous Workflow
- Object Orientation
- .NET Interoperability

Comparisons With Other Languages (C#,VB,Java)
- By Features
- By Code Segments

Summary
- F# Keywords
- F# Syntax

Appendix
- Using F# in Mac OSX

References

October 11, 2008

Installation of F#

To get start with learning F#, the first thing you need to do is to install it in your own computer so that you can try it out by yourself.

Here are the steps:

1. Download F# September 2008 CTP (Community Technology Preview)
2. Download .NET Framework 2.0
3. Download Visual Studio Shell 2008 (if you don't have Visual Studio 2008 Standard or above)

You should install .NET, VSS and F# in sequence.

Note that in the step 3, when you execute the downloaded binary, it actually expands into a directory. You have to run the setup inside again.

Although Visual Studio Shell is optional, it is highly recommended to be installed as it provides a more interactive environment for you to try out the code segments.

You should install VSS first so the F# can be integrated into the Shell. Note that Visual Studio Shell does not provide Windows Forms designer (but actually you can still create forms programmatically).

Try the following Hello World program and hit F5:

#light
printfn "Hello, World"

Congratulation for writing your first F# program ! You are now at the entry of the wonderland of F#.

One more thing - if you are like me as a Mac user (or you are a Linux person), you can actually install F# in Mac (and Linux) as well by using Mono 2.0 because F# is actually implemented in .NET !

Read this post for more information.

As I write this post, Mono 2.0 has just been released on Oct 6, 2008.


Reference:

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

October 9, 2008

Why F# Matters


The following is an excellent article that provides a detail list of rationale why it bothers to learn F# (and other functional programming languages):

Functional Programming For The Rest of Us

Here are some hallmark features of FP:

1. Immutability - "Variables" are unchangeable (so we call them values in FP) which implies thread safety and provision for concurrency.
2. Function As Value - Functions can be passed as arguments
3. Lazy Evaluation - Defer of execution until is necessary
4. Pattern Matching - Much powerful than the ordinary Select...Case.. statements
4. Succinct Syntax - Write terse programs
5. Unit Tests - Testing the functions as individual units are quite natural during the course of development

A bonus to F# is the first class citizenship in the .NET framework world. It certainly will be a member of Microsoft Visual Studio in the near future:

http://www.eweek.com/index2.php?option=content&task=view&id=49514&pop=1&hide_ads=1&page=0&hide_js=1


October 7, 2008

A Voyage to Functional Programming in F#

I have been programming for more than twenty years. Starting from BASIC in Apple II and CP/M, I am now working on VB.NET for most of the time in my job.

I had learned C and then C++ more than ten years ago. I found object-oriented programming great as it delivered a more power and productive paradigm. But in recent years, I realized that object-oriented programming in certain sense is merely an evolution from the imperative/procedural programming paradigm.

OOP definitely enables us to handle a higher level of complexity than plain procedural languages. Encapsulation, inheritance and polymorphism are powerful tools that I used all the time. I especially keen on building libraries to boost the performance of my team. In fact, I had built a few frameworks: one for C++ and at least two others for VB and VB.NET

However, under the hood, inside the library, everything is still so mechanical and in certain sense, low level. I tried hard to encapsulate boring logics (like copying values from screen controls to a database record and vice versa) to make building a business application more interesting (well, at least not boring) but I found that it is still a huge effort to build a full fledge application. Even with a powerful IDE like Microsoft Visual Studio, I found we still have a lot to do in order to deliver a solid application for my end-users.

We simply need more productivity and more important is to reduce the complexity in the deliverable as we need to maintain them afterward.

So I started a research on what other alternatives do we have.

Java simply is not what I am looking for as from my point of view, VB.NET/.NET Framework and Java are just the same thing from different vendors. Personally I think C# is a better choice than Java (there are free implementations of C# in various platforms) as at least I have a very powerful IDE.

I took a look in several (radically different) alternatives including Smalltalk and LISP. Smalltalk is probably the mother of all OOPs. People said that Smalltalk is productive and modern OOP languages still cannot fully mimic it. It is succinct and terse. I had tried to download one implementation to take a look. It looks quit special that it doesn't like the programming languages we use before (where source codes are plain text files). It presents in term of something like a virtual machine image.

A poor analogy is MS Access. You get everything inside a mdb file. When you quit, you dump the executing environment into a file.

Personally the Smalltalk environment is not appealing to me. I don't like the UI very much. It doesn't look native in any platform including Mac and Windows.

Smalltalk definitely is a neat and compact language that worth for further study in order to understand the origin of OOP better.

I had also realized that a powerful language still requires a comprehensive library. Smalltalk does have a huge library. But I need to invest a lot of time to learn a completely different library.
In addition, we need a solid database back-end for keeping persistent data. Smalltalk has its own way to handle persistence but it is not the mainstream way (client/server).

I peered some information on LISP but I am scared by the parentheses (even I am a C/C++ programmer as well). LISP is also a terse programming language but I think it is too difficult and the programs are simply not legible to most people.

Finally I settled on F#. Both LISP and F# are belong to functional programming (FP) paradigm.

Functional programming is a complete different paradigm from imperative (and OO) programming.

I found it terse as well yet I can understand it (though some of the concepts are new to me).

The advantage is it can use .NET Framework. Thus it gives me a break from OOP and yet taps the power from the familiar .NET Framework.

Therefore I decided to dive into it to seem whether functional programming can deliver its claims in terms of productivity and expression power.

I would like to share my findings in this blog to those interested in learning functional programming.