F#ing brilliant

I think there’s a time for all of us when we sat down to write a piece of code and it worked perfectly first time, and we thought to ourselves that perhaps we might just make developers of ourselves yet – shortly before we found an edge case that wasn’t covered, or irreparably broke everything with our overconfidence.

I’ve recently been going through Project Euler problems again – having solved a great many of them in C# – and solving them in F#. And it’s been amazing. That feeling of things working first time has become more commonplace than it has any right to be, particularly in a language that I’m (relatively) unfamiliar with and when I’m writing code with no IDE niceties (LinqPad); and since I’m fairly certain that I haven’t become insanely competent at overnight, I can’t help but feel that it can only be attributed to the language and paradigm itself.

If I wasn’t before, I’m certainly feeling the love for F# now.

For example…

Problem 13 is incredibly simple if you have an arbitrary-sized integer type, and while I could easily use Math.bigint, I thought it might be a nice exercise to create my own. Now, the code below may not be the prettiest in the world, and it certainly isn’t the most efficient, but for something that I knocked together in a few minutes in my spare time without taking too much care over… it worked first time! (Well… almost anyway… the first time around I forgot the case where the digits had all been dealt with but carried wasn’t zero.) Hopefully that doesn’t undermine my point too much, because I’ve truly been impressed by how much I feel helped rather than hindered by the language.

type BigNum (digits: int list) = 
	// digits: list of decimal digits smallest-biggest
	static let carryDigits digits = 
		let rec carryDigits digits processed carried = 
			let getDigitAndCarry next = 
				next % 10, next / 10
			match digits with
			| [] -> 
				match carried with
				| 0 -> processed // all done and nothing more to carry
				| _ -> // digits all done, but still carrying left
					let digit, carriedNext = getDigitAndCarry carried
					carryDigits digits (digit :: processed) carriedNext
			| _ -> // keep going...!
				let next = (List.head digits) + carried
				let digit, carriedNext = getDigitAndCarry next
				carryDigits (List.tail digits) (digit :: processed) carriedNext
		carryDigits digits [] 0 |> List.rev
	member this.digits = carryDigits digits
	static member Create (s: string) =
		let numStringToList (s: string) = 
			|> Seq.map (string >> Int32.Parse)
			|> List.ofSeq
			|> List.rev
		let digits = numStringToList s
	static member (+) (a: BigNum, b: BigNum) =
		let matchLengths list1 list2 = 
			let extend list toLength = 
				let lengthToAdd = max 0 (toLength - List.length list)
				let zeroes = [for i in 1..lengthToAdd -> 0]
				list @ zeroes
			let list1' = extend a.digits (List.length list2)
			let list2' = extend b.digits (List.length list1)
			list1', list2'
		let resultDigits = 
			matchLengths a.digits b.digits 
			||> List.zip 
			|> List.map (fun (a,b) -> a + b)
			|> carryDigits
	override this.ToString() =
   		List.fold (fun acc x -> string(x) + acc) "" digits


Being ill sucks

Last week I was really very ill – something flu-like – and this week I’m slowly recovering from whatever it was. Beside sapping all of my energy it’s also completely messed with my ability to actually think. Because of that, I haven’t really done anything particularly interesting – I’ve been using this time to do all of the boring / monotonous tasks that I’d been putting off – and haven’t had any particularly exciting insights into, well, anything really.

Never too ill for a monad

Since I’ve not really done much, and I’m not feeling up to writing about anything particularly involved, here’s a very simple error monad-like-structure I threw together this week for something I was working on.

public class Exceptional<TValue>
	#region Fields

	private readonly TValue value;
	private readonly Exception exception;
	private readonly bool hasResult;


	#region Constructors

	private Exceptional(TValue value)
		this.value = value;
		hasResult = true;

	private Exceptional(Exception exception)
		this.exception = exception;
		hasResult = false;

	#region Methods

	public Exceptional<T> Apply<T>(Func<TValue, T> func)
		return UnWrap(
			v => Exceptional<T>.Wrap(() => func(v)),
			ex => new Exceptional<T>(ex));

	public T UnWrap<T>(
		Func<TValue, T> whenResult,
		Func<Exception, T> whenException)
		return hasResult
			? whenResult(value)
			: whenException(exception);

	public static Exceptional<TValue> Wrap(Func<TValue> func)
			return new Exceptional<TValue>(func());
		catch (Exception exception)
			return new Exceptional<TValue>(exception);


The brain is a muscle…

So, even though I really wasn’t feeling particularly up to it, I wrote something similar to the above in F#. I’m trying to slowly get a feel for the language after years of OOP indoctrination, and this is (sadly) probably the biggest thing I’ve written in F# so far. I love the concepts behind the language but it can be incredibly frustrating to move from being able to produce clean, concise, idiomatic code in C# to struggling to write even the simplest structures in F#.

type Exceptional<'a> = 
	| Value of 'a
	| Ex of Exception

// (a -> b) -> (a -> Exceptional<b>)
let Bind f = 
	fun a -> 
		try Value(f a) 
			| ex -> Ex(ex)

let incr x = 
	match x with 
		| 7 -> 	raise (Exception "Seven Eight Nine")
		| _ -> 	x + 1
let _incr =  Bind incr

_incr 3 |> Console.WriteLine
_incr 7 |> Console.WriteLine