Power of Haskell

In: C#|Functional Programming|Haskell

27 May 2011

Learning haskell recently has really highlighted the power of the built in classes of haskell.

Compare a function in haskell to captialize the first letter in a sentence, vs c#.

Haskell

1
2
3
4
5
import Data.Char
 
sentenceCase :: [Char] -> [Char]
sentenceCase [] = []
sentenceCase (x:xs) = [toUpper x] ++ map toLower xs

I think to be fair I should explain what the above function does.

  1. The first line is the type definition, states that the function will accept a list of chars (a string) and return a list of chars.
  2. The second line is a pattern match, in that if an empty string is provided then it will return an empty string.
  3. The third line is also a pattern match, however will only be accepted if the first pattern is passed over
  4. So in this case it will be run when accepting a non-empty string as it’s input parameter.
Function Description
sentenceCase The name of the function
(x:xs) The brackets are grouping the notation x:xs together so it’s evaluated as a single argument.
x:xs Is actually a special list shorthand that says for list xs, let the value x be the first element and xs be the remainder. Additionally this could be expressed as a:b:c:xs, abc representing the first 3 elements of the list and xs representing the remainder.
[] The left and right square brackets represent a list, so in this case we’re creating a new list filled with 1 character which will be uppercase.
toUpper From the Data.Char library accepts a Char and returns an upper case Char
toLower From the Data.Char library accepts a Char and returns a lower case Char
map Is a built in library function which performs a function on each individual member of a list and returns a resulting list. ie map (\x -> x * 2) [1,2,3] will return a list with the function (x * 2) applied to each of its elements [2,4,6]
++ Concatenates two lists.

So in 1 line of code you could read the above function as taking a list of characters, return the first element as uppercase and the remainder as lowercase.

Vs C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static string ToSentenceCase(this string original)
{
	return String.IsNullOrEmpty(original) 
		? original 
		: string.Format("{0}{1}", Head(original).ToUpper(), Tail(original).ToLower());
}
 
public static string Head(this string original)
{
	return String.IsNullOrEmpty(original) 
		? original 
		: original.First().ToString();
}
 
public static string Tail(this string original)
{
	return String.IsNullOrEmpty(original) 
		? original 
		: original.Substring(1, original.Length - 1);
}

To be fair, c# doesn’t have a Head or Tail method built in. However even using extension classes, the built in pattern matching from Haskell seems to be a far nicer solution language wise than c#.

Is there a way to refactor this c# code futher?

2 Responses to Power of Haskell

Avatar

Rob Gray

May 27th, 2011 at 11:04 pm

Why would you make such a thing ? :p

Avatar

Justin

May 27th, 2011 at 11:40 pm

Oh I dunno… whimsy?

I’m sure it’ll be such a popular request, that they’ll add it to the CSS specification at some point.

text-transform: sentence-case. 😀

Comment Form

About Justin

justin

Justin is a Senior Software Engineer living in Brisbane. A Polyglot Developer proficient in multiple programming languages including [C#, C/C++, Java, Android, Ruby..]. He's currently taking an active interest in Teaching Kids to Code, Functional Programming, Robotics, 3D Printers, RC Quad-Copters and Augmented Reality.

About This Blog

Software Engineering is an art form, a tricky art form that takes as much raw talent as it does technical know how. I'll be posting articles on professional tips and tricks, dos and donts, and tutorials.

profile for Justin Shield on Stack Exchange, a network of free, community-driven Q&A sites

Photostream

  • What I look for in a senior software engineer Justin Shield: […] I’m not going to list the design patterns that you’ll need, I’ve already [...]
  • Justin: Hi Ross, I do actually like Umbraco, it provides some nice abilities for creating websites that I [...]
  • Justin: Hi GordonBGood, Thanks for taking the time in replying. You're absolutely correct, it is turners s [...]
  • Ross Gallagher: Hi Justin, I'm a fellow Aussi looking to use Umbraco to create a simple website. I have downloaded [...]
  • GordonBGood: This is the "Turner Sieve" which **IS NOT** the Sieve of Eratosthenes (SoE) neither by algorithm nor [...]