So I love this example that I saw in one of my daily programming newsletters – F# interactive in the browser – no need for VS.

Check it out here…

So I have put up the slides from my ten minute presentation on 1 best practice in F# up for anyone who wants them. Also find a link to Bart De Smet’s presentation on Functional Programming ......

Well after a hectic week of work and studying I eventually had some me time… today I thought I would attempt Euler Problem 16. The Problem 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? The Solution So I thought I would get to concerned about length of code, but rather focus on implementing several practices that I have learnt in the last couple of weeks, namely tail recursive functions and composite functions… I initially broke ......

A few months ago I had a really frustrating debate with my younger brother. He had come up to JHB to come for a visit and we decided to talk about programming. Of course I thought I would put a good pitch in for F#, but just couldn’t seem to do it any justice. Eventually his point was as follows - “What really is the difference between declaring a functional solution vs an iterative solution. Sure, in F# you have something like the Seq.map function, but isn’t it just a shorthand for a for loop or ......

So today I had the pleasure of attempting Euler Problem 10… the problem goes as follows… The Problem The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. The Solution What I enjoyed about this problem is that if you have done the previous Euler problems, then this problem is really just a focus on pure performance as the other aspects of the problem have in essence been solved before. Not that I have found an optimal solution, in fact I was very ......

For those using VS2010 with F#, this may be a useful reference for the default keyboard shortcuts in VS.

“prefer active patterns over multiple when guards” In line with my previous posts on 5 best practices for F# from Daniel Mohl’s slideshow, over the last few days I have been learning about active patterns. The exact best practice is to prefer active patterns over multiple “when” guards during pattern matching and in this post I am going to try and compare the two techniques and then also explain some of the basics of active patterns and why it is called active. Now before I go any further, let me ......

Another day flown by… another Euler problem to tease me… today I attempted problem 9. The Problem A Pythagorean triplet is a set of three natural numbers, a < b < c, for which a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 +16 = 25 = 5^2. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. The Solution Initially I thought I would try and tackle this problem by setting up 3DArrays in F#, thus my question on StackOverflow regarding 2D Array. However, if ......

Today I had some more spare time to give Euler a go and thought I would try my luck on problem 8. The Problem Find the greatest product of five consecutive digits in the 1000-digit number. 731671765313306249192251196... 969835203127745063262395783... 858615607891129494954595017... 125406987471585238630507156... 668966489504452445231617318... 622298934233803081353362766... 303589072962904915604407723... ......

In line with Daniel Mohl’s presentation, the third best practice for F# is Tail Recursive Functions. I must admit, while I did go over recursion several years ago, I never heard (or possibly ignored) any mention of tail recursive functions… so what exactly is a tail recursive function? I posed the question on Stack Overflow and got some great examples and responses… Possibly the best explanation came from a post done by Chris Smith entitled Understanding Tail Recursion. Basically, non tail recursive ......

In the last few months since I started learning F# I have begin to notice an increase in the number of people blogging about the language. Sure, it could just be that I am noticing it more because I am actively looking out for questions and blog posts, but even in my day to day reading of Code Project Daily News and Stack Overflow questions there seems to be increased activity around the language. So what sparked this post? Well, today today I logged in and saw that the latest podcast by DNR was ......

These last few days I attempted Euler Problem 7. It has been an interesting experience since it is the first real Euler problem that I initially attempted to tackle recursion for a solution but eventually bypassed as I found what I considered a simpler way to solve the problem. In addition to recursion, it was also interesting to see performance issues occur, and how a better understanding of what some of the Seq functions does can dramatically effect the over all performance of the solution. The ......

Daniel Mohl put up a great slide presentation on the 5 best practices for F#. Since I am new to the language I thought I would go through each practice and explore it a bit as separate posts. Best Practice 1 – Prefer Short Functions with only one primary responsibility As Daniel points out, functions that follow this practice enable single responsibility pattern (SRP). function composition I must admit that up to this point, I have never really looked formally at what the SRP or Function Composition ......

Today I was doing some work work and kept having to parse a log file. After a few hours of doing the same routine stuff I thought I might try and automate it using F#. It was depressingly simple to achieve… Example Extract of Text File *ERROR* : Drawing num {^BASE^2 }783, surface num 1 uses invalid real colour number 2 _warning_ : The base file has 171 DLT types, the main file has 126. _warning_ : AutoPlan error: There are no base corner units with autoplan 30 (see theme *ERROR* : Drawing num {^BASE^2 ......

I had the following code snippet… #light open System let ExeC = printfn "c" 3 let ExeB b = printfn "b" 2 let ExeA = printfn "a" 1 printfn "Example %d " ExeA printfn "Example %d " (ExeB 1) printfn "Example %d " ExeC Console.ReadLine() With the output as follows… c a Example 1 b Example 2 Example 3 Initially the execution of the code seemed unusual… up to now I would have expected ExeC, ExeB & ExeA to all be functions, but a previous comment from Brian indicated that there was a difference. StackOverflow ......

So I have mentioned lazy functions before, in particular related to sequences, however I came across it again – but this time with lazy values and thought I would explore it a little bit more… I used the following code snippet… #light open System let lHelloWorld = lazy(printfn "Lazy Hello World"; 30+30) let aHelloWorld = (printfn "Active Hello World"; 30+30) printfn "lazy value is %d" lHelloWorld.Value printfn "lazy value is %d" lHelloWorld.Value printfn "active value is %d" aHelloWorld printfn "active ......

I had a bit of a no brainer yesterday… I have been using the forward pipe to convert values, and came up against the problem where the following code would work let IntToFloat = 10 |> float let FloatToInt = 10.0 |> int But the equivalent bigint code would not work… let FloatToBigint = 10.0 |> bigint It was only after a few hours of reading up on type casting in F# and getting totally confused that I finally resulted to Stack Overflow. The answer was humbling… The reason why FloatToBignint ......

So I must admit, the first time I saw it I scratched my head and thought to myself, why will I ever use that? Several months since then and now I love the Forward Pipe (FP) in F#. Why? For me, the the FP makes me think of a problem in a different way. In C# when I attempt to solve I problem I typically break it down in steps, once I have reached the end result I have my smallest step, and then I work my methods backwards so that I can get back to the original input… in a very real way it makes me ......

Today I had a quick bash at problem 6. To me this has been the easiest of the problems so far. It is the first time though that I got to use the Seq.Fold function which was good to get exposure to. I am not to happy with my code though, I can’t explain it, but it just seems a bit messy… Anyhow, I look forward to feedback from everyone in the F# community on their approaches. Problem The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of the sum of the ......

As per the post I decided to do a F# attempt at FizzBuzz. It took a few seconds… Using FizzBuzz to Find Developers who Grok Coding.. let FizzBuzz = seq {1..100} |> Seq.iter (fun x -> match x with | x when x % 5 = 0 && x % 3 = 0 -> printfn "FizzBuzz" | x when x % 3 = 0 -> printfn "Fizz" | x when x % 5 = 0 -> printfn "Buzz" | _ -> printfn "%d" x) ......

So today I tackled a Euler problem that I had originally looked into several months back when I initially started exploring F# Problem 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? Solution I decided on the brute force approach again. I believe that I could have worked with prime factors etc to have reached the solution a lot quicker – but ......

The last few days I began to play around with problem 4 of Euler. I really enjoyed this problem since it dealt with a few functions in F# that I haven’t dealt with in the past. Problem A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99. Find the largest palindrome made from the product of two 3-digit numbers. Solution So I divided the problem up into a few functions… reverseString simply reverses a string. isPalindrome ......

So I have been playing around with more Euler problems and I have found it a great way to learn some of the basic functions of F# and Seq specifically. The more I play the more I am really loving F# and how succinct the language is. Today I thought I would put up a brief post on the Seq.unfold function. So, I see this function being used everywhere… my understanding of it is that it is a way that one can build a sequence based on a series of commands. I will use a small code snippet to show an example ......

So this weekend I made an attempt at problem 3 of Project Euler. Problem 3 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? My Solution // // Checks whether a number is prime or not using the Trial Division approach // To improve performance I have only pass through odd numbers greater than 2 and do not check for this case // let isPrime (number : bigint) = match number with | _ -> seq { bigint(2) .. bigint(1) .. bigint (Math.Sqrt (float ......

Today I thought I would give a bash at problem 2 of Euler. Problem 2 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Find the sum of all the even-valued terms in the sequence which do not exceed four million. My Solution (Brute Force) So first of all I needed a Fibonacci generator. I resorted to one that recursively defines the Fibonacci number based on the index with ......

Every now and then I give project Euler a quick browse. Since I have been playing with F# I have found it a great way to learn the basics of the language. Today I thought I would give problem 1 an attempt… Problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. My F# Solution I broke this problem into two functions… 1) be able to generate a collection of numbers ......

I did a recent post on implementing WPF with F#. Today I would like to expand on this posting to give a simple implementation of the MVVM pattern in F#. A good read about this topic can also be found on Dean Chalk’s blog although my example of the pattern is possibly simpler. With the MVVM pattern one typically has 3 segments, the view, viewmodel and model. With the beauty of WPF binding one is able to link the state based viewmodel to the view. In my implementation I have kept the same principles. ......

Today I had a few minutes in the evening to go over my original Adam and Eve code… what I wanted to see tonight was if I could optimize the code any further… which I was pretty sure could be done. Ultimately what I wanted to find from the experiment was a balance between optimized code an reusable code. On the one hand I can put everything into a single function and end up with a totally unusable function that is extremely compressed, which would have big comebacks when making modifications at a ......

I was listening to Dot Net Rocks show #560 about F# and during the podcast Richard Campbell brought up a good point with regards to F# and a GUI. In essence what I understood his point to be was that until one could write an end to end application in F#, it would be a hard sell to developers to take it on. In part I agree with him, while I am beginning to really enjoy learning F#, I can’t but help feel that I would be a lot further into the language if I could do my Windows Forms like I do in C# ......

This evening I had the pleasure of reading one of ThomasP’s blog posts on first class events. It was an excellent read, and I thought I would make a brief derivative of his post to explore some of the basics. In Thomas’s post he has a form with an ellipse on it that when he clicks on the ellipse it pops up a message box with the button clicked… awesome. Something that got me on the post though was the code similar to the one below… // React to Mouse Move events on the form let evtMessages = frm.MouseMove ......

Another day gone by looking into F#. Today I thought I would ramble on about lists and arrays in F#. Coming from a C# background I barely ever use arrays now days in my C# code – why you may ask – because I find lists generally handle most of the business scenario’s that I come across. So it has been an interesting experience with me keep bumping into Array’s & Lists in F# and I wondered why the frequency of coming across arrays was so much more in this language than in C#. Take for instance ......

So I am loving my Expert F# book. I wish I had more time with it, but the little time I get I really enjoy. However today I was completely stumped by what the book was trying to get across with regards to pattern matching. On Page 38 – Chapter 3, it briefly describes F# option values. On this page it gives the code snippet along the code lines below and then goes on to speak briefly about pattern matching... open System type 'a option = | None | Some of 'a let people = [ ("Adam", None); ("Eve", None); ......

Up to now I have been looking at F# for modular solutions, but have never considered writing an end to end application. Today I was wondering how one would even start to write an end to end application and realized that I didn’t even know where the entry point is for an F# application. After browsing MSDN a bit I got a basic example of a F# application with an entry point [<EntryPoint>] let main args = printfn "Arguments passed to function : %A" args // Return 0. This indicates success. 0 Pretty ......

I was browsing StackOverflow this weekend and came across a great response to the question “What is a good way to get started in F#”.

Find the question & answer here.

Okay, so the title may be a little misleading… but I am currently travelling and so have had very little time and access to resources to do much fsharping – this has meant that I am right now missing my favourite new language. I was interested to see this post on Stack Overflow this evening concerning the performance of the F# language. The person posing the question asked 8 key points about the F# language, namely… How well does it do floating-point? Does it allow vector instructions How friendly ......

Okay, some basic examples of overloading in F# Overloading Constructors Assume you have a F# object called person… type Person (firstname : string, lastname : string) = member v.Fullname = firstname + " " + lastname This only has one constructor. To add additional constructors to the object by explicitly declaring them using the method member new. type Person (firstname : string, lastname : string) = new () = Person("Unknown", "Unknown") member v.Fullname = firstname + " " + lastname In the code ......

In the next few blog posts I am going to explore objects in F#. Up to now, my dabbling in F# has really been a few liners and while I haven’t reached the point where F# is my language of preference – I am already seeing the benefits of the language when solving certain types of programming problems. For me I believe that the F# language will be used in a silo like architecture and that the real benefit of having F# under your belt is so that you can solve problems that F# lends itself towards and ......

So I have been delving into F# more and more and thought the best way to learn the language is to write something useful. I have been meaning to get some more Silverlight knowledge (up to now I have mainly been doing WPF) so I came up with a really simple project that I can actually use at work. Simply put – I often get support calls from clients wanting new activation codes. One of our main app’s was written in VB6 and had its own “security” where it would require about a 45 character sequence for ......

So in part one of my posting I covered the real basics of object creation. Today I will hopefully dig a little deeper… My expert F# book brings up an interesting point – properties in F# are just syntactic sugar for method calls. This makes sense… for instance assume I had the following object with the property exposed called Firstname. type Person(Firstname : string, Lastname : string) = member v.Firstname = Firstname I could extend the Firstname property with the following code and everything would ......

First of all – shout out to Peter Adams – from the feedback I have gotten from him on the last few posts of F# that I have done – my mind has just been expanded. I did a blog post a few days ago about infinite sequences – I didn’t really understand what was going on with it, and I still don’t really get it – but I am getting closer. In Peter’s last comment he made mention of Lazy Evaluation. I am ashamed to say that up till then I had never heard about lazy evaluation – how can evaluation be lazy? ......

So I have seen a few posts done by other F# fans on solving project Euler problems. They looked really interesting and I thought with my limited knowledge of F# I would attempt a few and the first one I had a look at was problem 5. Which said : “2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?” So I jumped into coding it and straight away got stuck – the ......

I am plodding along with my F# book. I have reached the part where I know enough about the syntax of the language to understand something if I read it – but not enough about the language to be productive and write something useful. A bit of a frustrating place to be. Needless to say when you are in this state of mind – you end up paging mindlessly through chapters of my F# book with no real incentive to learn anything until you hit “Exceptions”. Raising an exception explicitly So lets look at raising ......