‘F#’ Category Archives
May
Real world functional programming
by Mikael Lundin in F#, Programming
Today I held a seminar about functional programming at Valtech Tech Day. It’s no coincidense that the title of the seminar is the same as the book by Tomas Petricek, since it inspired me to do the talk. After playing around with F# for over a year, it’s time to get serious and use it in production case.
You will find my slides here. (or download as pdf)
Do we need functional programming?
Yes! Our CPU’s aren’t getting faster, but better at doing several things at the same time. That is why we need to focus on parallel programming, and parallel programming is hard to do with imperative programming. That is why we must rethink our problem definitions and make them much more expressive, solving our problems with functional concepts.
What functional programming language should i learn?
If you’re on .NET today, F# will be the shortest path to functional programming for you. If you’re a hardcore Java developer you should look into Clojure. Erlang seems to be the coolest functional language right now, but Scala is also an alternative.
Is recursion the answer to everything?
Some people does not like recursion because they’re so used to iterative way of thinking. There’s nothing hard about recursive programming, but your brain is not tuned for it. Recursion is not the solution to everything, just as the foreach/while-loop is not the answer to everything, but I would say that its just as important as a looping construct.
Monads, gonads?
Ehhm … yeah.
Apr
Option type implementation in C#
by Mikael Lundin in F#, Programming
F# has this clever functionality called Option<’a>. This means that, instead of returning null from a function, you return an option. This option could have the value Some x or None, where x is the value you want to return, clearly indicating that this method could return a value or not.
let getIndexOfSubstring (s : string) (substring : string) =
let index = s.IndexOf(substring)
match index with
| -1 -> None
| n -> Some n
Function signature:
val getIndexOfSubstring : string -> string -> int option
And for those of you not fluent in F#, this means that getIndexOfSubstring takes two strings and returns an option of int. This option could be Some int, or it could be None if the substring is not found.
What you win, is that option is now part of the method signature. As a method invoker you will have to handle the None option. As with NULL references, a null return value is often a side effect of the method and often unexpected.
Implement Option<’a> in C#
The option type is a type that we use as return value from a method.
public Option<int> GetIndexOfSubstring(string s, string substring)
{
var index = s.IndexOf(substring);
if (index == -1)
return new None<int>();
return new Some<int>(index);
}
What does this mean?
- The implementation clearly states that the method returns some value or no value at all.
- If the return type is an option, you need to return both Some and None for the construct to be valid. The caller of this method expects that both Some and None are possible values.
The method signature also provides you with better test names.
[Test]
public void ShouldReturnSomeIndexForExistingSubstring()
{
/* Test implementation */
}
[Test]
public void ShouldReturnNoneWhenSubstringDoesNotExist()
{
/* Test implementation */
}
What are Some and None?
The example code makes much more sense if you look at the class diagram of Some/None.
The code for the option is very abstract.
// Used as return type from method
public abstract class Option<T>
{
// Could contain the value if Some, but not if None
public abstract T Value { get; }
public abstract bool IsSome { get; }
public abstract bool IsNone { get; }
}
We could implement IsSome/IsNone by comparing this type with Some/None class, but I don’t like the idea of a superclass reference any subclass.
The implementation of Some and None are pretty straight forward.
public sealed class Some : Option
{
private T value;
public Some(T value)
{
// Setting Some to null, nullifies the purpose of Some/None
if (value == null)
{
throw new System.ArgumentNullException("value", "Some value was null, use None instead");
}
this.value = value;
}
public override T Value { get { return value; } }
public override bool IsSome { get { return true; } }
public override bool IsNone { get { return false; } }
}
public sealed class None : Option
{
public override T Value
{
get { throw new System.NotSupportedException("There is no value"); }
}
public override bool IsSome { get { return false; } }
public override bool IsNone { get { return true; } }
}
Creating a Some instance with null value is only ridiculous, and that is why we throw an exeption. The same goes for calling Value on None.
How do you call a method that returns Option<T>?
Here is some code that will call my first example and act differently if the result is Some or None.
// Get string before substring
private string SubstringBefore(string s, string substring)
{
var operations = new StringOperations();
var index = operations.GetIndexOfSubstring(s, substring);
if (index.IsSome)
return s.Substring(0, index.Value);
return s;
}
What are the benefits of the calling method?
- The result must immediately be checked if it is Some/None before you start using the value. Of course you could ignore the check and go directly to index.Value if you’re willing to take the exception when index is None. (just like null values)
- It’s clear for the reader that GetIndexOfSubstring might not return a value and that has to be dealt with.
Using Option<T> with reference types
Value types like int already have this functionality with Nullable<T>. Nullable works the same way with a different purpose, to give value types a null value.
With value types it is quite clear that “null” means “no value”, but with reference types it could mean
- Abscense of value. The method states that for given input there is no output value.
- Empty set. Specially working with databases, null could mean that the result set was empty.
- Unknown. The method does not know how to respond and throw us a null (when it really should throw an exception)
- Not initialized. An object has not been initialized and the reference is null.
The real danger of null in .NET is when it comes from the framework or a third part library and we where not expecting it. That is when you’ll see the NullReferenceException, the most – and it could pop up at any time in production.
This is why we don’t allow null values into Some. Better to fail early when we’re creating the result set of the method, than letting the program run in a faulted state until it tries to use that value.
public Option<User> FindUserByName(string name)
{
var query = from user in Users
where user.FirstName.Contains(name) || user.Surname.Contains(name)
select user;
var found = query.FirstOrDefault();
if (found == null)
return new None<User>();
return new Some<User>(found);
}
What has to be noticed in this example, is that found really have to be checked for null before entered into Some, or it may blow up. This means that Some/None null checks would be all over the place violating DRY. Could we fix it with an extension method?
public static class OptionExtensions
{
public static Option<T> SomeOrNone<T>(this T reference)
where T : class
{
if (reference == null) return new None<T>();
return new Some<T>(reference);
}
}
And this changes the previous example to.
var found = query.FirstOrDefault(); return found.SomeOrNone();
When is it elegible to return Some<T> instead of Option<T>?
When we have a reference return type that we want to communicate, “could never be null”, we could use Some as the return type, but this would feel a bit weird at the method invokers end.
You could communicate the same thing with Microsoft Code Contracts.
Here’s really three possible state of Option<T>, Some/None and Null. How do I protect myself from a method with Option<T> return type, from returning null?
Microsoft Code Contracts is also the answer here, or you could look into AOP and write an aspect that will throw an exception when you try to return null instead of an instance of Option<T>.
If you’ve decided on the method signature, you probably also agree on the pattern Some/None. But the method signature could be forced upon you with an interface, and in that case some security measure that makes sure that you don’t return null could be useful.
All the source code in a nice packaged VS2010 solution can be downloaded from here.
Mar
Feb
Citerus programming challenge
by Mikael Lundin in F#, Programming
I’m very weak for programming challenges. Citerus held a contest where you could win an iPad if you would solve their problem. Since I’m a .NET developer, I’m not elegible for winning such a challenge but I love to meet a challenge just for the sake of it.
The Challenge
Given a string
VOCDIITEIOCRUDOIANTOCSLOIOCVESTAIOCVOLIOCENTSU
And a collection of abbreviations
TDD, DDD, DI, DO, OO, UI, ANT, CV, IOC, LOC, SU, VO
What is the shortest string you could produce by removing abbreviations from it?
Example
1) ...BIDDDOCDDD... (remove DDD) 2) ...BIDDDOC... (remove DDD again) 3) ...BIOC... (remove IOC)
The solution
This kind of problem is ideal for F#.
The main problem is that you just can’t remove abbreviations, because you won’t find the shortest string. You will have to try to remove abbreviations in all orders to find the shortest string produced. I do this by recursive calls, where every abbreviation removal is a path in the tree. I solve the problem by taking the branch with the shortest result.
// http://www.citerus.se/jfokus
module CiterusChallenge
// In the string
let s = "VOCDIITEIOCRUDOIANTOCSLOIOCVESTAIOCVOLIOCENTSU"
// Any occurrence of
let abbreviations = ["TDD"; "DDD"; "DI"; "DO"; "OO"; "UI"; "ANT"; "CV"; "IOC"; "LOC"; "SU"; "VO"]
// Should be removed
let rec remove (abbreviations : string List) (s : string) =
// Collect any version where one abbr is removed from s
// Filter out those with no effect on s
let collect =
abbreviations
|> List.map (fun abbr -> s.Replace(abbr, ""))
|> List.filter (fun short_s -> short_s.Length < s.Length)
// Select longest string of s1 and s2, or s1 if they're equal
let min (s1 : string) (s2 : string) =
if s1.Length <= s2.Length then s1 else s2
// Match result of abbreviations removal
match collect with
| [] -> s
| _ -> List.fold ( min ) s (collect |> List.map ( remove abbreviations ))
// Execute
let solution = lazy ( s |> remove abbreviations )
Utilities.measure_execution_time solution |> ignore
// Tests
let tests =
"HELLO" = ("HETDDLLDIO" |> remove abbreviations ) && // Two consecutive abbr
"HELLO" = ("HEDTDDILLO" |> remove abbreviations ) && // Two nested abbr
"HELLO" = ("HEDTDDILLO" |> remove abbreviations ) && // Removing TDD before DI
"HELIUM" = ("HELANTDDDIUM" |> remove abbreviations ) && // Don't remove DI or TDD, remove ANT then DDD
"" = ("DTDDIIOC" |> remove abbreviations ) && // Take TDD then IOC last DI will leave only empty string
"B" = ("BIDDDOCDDD" |> remove abbreviations) // Some wierd example from the problem description
Look, F# with syntax highlighting! All in all, the algorithm code is 11 LOC, if you remove the comments, and runs for 320 ms on my machine.
Jan
Communicate your e-mail
by Mikael Lundin in F#, Programming
For anyone to contact you they need to know how. That’s why you should display your e-mail everywhere. Except … you will get a lot of spam because e-mail is not secure by default.
The e-mail protocols are flawed and that is why there are spammers, constantly scanning the web for new e-mail addresses for sending crap to.
What alternatives do you have communicating your e-mail address on the web, without handing it over to spammers framed in gold?
Content information as an image
Spammers are lazy. If it takes too much effort to extract an e-mail they will skip it, because there are millions more waiting. Easiest way to protect your e-mail address from spammers is to put it in an image like below.

Cons
- You can’t mark and copy the e-mail address into your e-mail client. You’ll have to type it out manually.
- The image can’t be percieved by blind people or other kinds of screen readers.
Use scripts to display e-mail
Since spambots very seldom execute client side scripts on a page it would be safe to create a placeholder on the page and replace the contents with the e-mail. You could do it with javascript, or Flash, Silverlight if you want to make it even harder to parse out it with a bot.
<html>
<body>
<span id="email">[Email Placeholder]</span>
<script type="text/javascript">
document.getElementById('email').innerText = "spam@litemedia.se";
</script>
</body>
</html>
Cons
- Today, most browsers are able to run scripts and most companies allows their employees to run javascript, but this is not unobtrusive javascript. If you turn of javascript, content of the page will change.
Convert to HTML Literals
Most of the bots are quite stupid. They download the html page and run a regex looking for e-mail addresses. If your e-mail address does not look like an e-mail address they will not find it. That’s why you could convert every character in your e-mail address to ascii html literals.
Here’s how to do the conversion in F#.
let encode (s : string) =
s
|> Seq.map (fun c -> System.String.Format("&#{0:d};", c |> int))
|> System.String.Concat
> encode "spam@litemedia.se";;
val it : string =
"spam@litemedia.se"
This is what my e-mail will look like after being rendered in a browser: spam@litemedia.se
Cons
- A really clever bot will look for the @ literal @ and parse out the rest of the e-mail address. That is however not very likely since it is too much work and there are millions of unprotected e-mails waiting.







