background preloader

F#

Facebook Twitter

Awesome F# - Decision Tre. In my previous post I went over the theory behind the ID3 algorithm.

Awesome F# - Decision Tre

Now that we got all that painful math out of the way, let’s write some code! Here is an implementation of the algorithm in F#. (It is also attached to this blog post, download it via the link at the bottom.) The entropy and informationGain functions were covered in my last post, so let’s walk through how the actual decision tree gets constructed. There’s a little work to calculating the optimal decision tree split, but with F# you can express it quite beautifully. let attributeWithMostInformationGain = attributesLeft |> List.map(fun attrName -> attrName, (informationGain data attrName)) |> List.maxBy(fun (attrName, infoGain) -> infoGain) |> fst First, it takes all the potential attributes left to split on… attributesLeft … and then maps that attribute name to a new attribute name / information gain tuple … |> List.map(fun attrName -> attrName, (informationGain data attrName)) |> fst The code is very straight forward.

Discriminated Unions in F# F# vs C# vs Java: Functional Collection Parameters. I wrote a post about a month ago on using functional collection parameters in C# and over the weekend Fabio and I decided to try and contrast the way you would do this in Java, C# and then F# just for fun.

F# vs C# vs Java: Functional Collection Parameters

Map Map evaluates a high order function on all the elements in a collection and then returns a new collection containing the results of the function evaluation. F# and LINQ. If you take a look under the hood at the new System.Query name space you will see that the core of it is located in the “Sequence” class and this class bears an amazing resemblance to the F# List library located in the MLLib.dll.

F# and LINQ

Okay you have to use your imagination a tiny bit, but if you consider that: System.Collections.Generic.IEnumberable<T>() Is really not that different from: Microsoft.FSharp.List<A> Then you start to see that: Object Oriented F# A post by Jeremy Miller caught my eye this morning in regards to extension methods in Javascript.

Object Oriented F#

While I think that's pretty interesting, I don't think it's a real fair comparison. Runtime Units of Measure for F# The F# compiler includes a Units of Measure feature which infers a measure type at compile time, which means you get measure type safety with uncompromised runtime performance.

Runtime Units of Measure for F#

Example of F#’s built-in Units of Measure feature (hover over shows inferred type): Sometimes you might also want to actually infer units of measure at runtime, say to display the inferred unit type at the UI. The following F# code prototype provides such inference. Lets start by defining some metres and seconds unit types: let m = UnitType.Create("m") let s = UnitType.Create("s") Units of Measure (F#) The previous syntax defines unit-name as a unit of measure. The optional part is used to define a new measure in terms of previously defined units. Units of Measure in F#: Part One, Introducing Units - Andrew Ken. Do you remember NASA's Mars Climate Orbiter?

Units of Measure in F#: Part One, Introducing Units - Andrew Ken

It was lost in September 1999 because of a confusion between metric and so-called "English" units of measurement. The report into the disaster made many recommendations. In all likelihood, the accident could have been prevented if the NASA engineers had been able to annotate their program code with units, and then employed static analysis tools or language-level type-checking to detect and fix any unit errors. Of course, the languages that NASA used had no language or tool support for units. Christmas tree in F# Accelerator and F# (I.): Introduction and calculating PI. Mapping the Reactive Framework (Rx) operators for F# » Rash thou. Observing asynchronous downloads with F# and the Reactive Extens. In one of my lasts posts I showed how we can transform some of the operators from the Reactive Framework for an easier use in F#.

Observing asynchronous downloads with F# and the Reactive Extens

This time I will demonstrate how we can use this API and asynchronous workflows to download a couple of websites asynchronous and in parallel. First of all we create a synchronous download function: let SyncHttp (url:string) = let request = WebRequest.Create url let response = request.GetResponse() use stream = response.GetResponseStream() use reader = new StreamReader(stream) let result = reader.ReadToEnd() url,result.

Inline F# in PowerShell. December 31, 2008 at 7:12 AM — Andy Schneider We can use C# quite easily with the Add-Type Cmdlet.

Inline F# in PowerShell

With the -language parameter, you can also use VB .NET. However,if you are into functional programming and like F#, you can use that as well, although not quite as easily. First, go and download the September 2008 CTP of F#. Once you have this installed, launch the 32 bit version of PowerShell. The F# CodeDom.dll was installed in C:\Program Files (x86)\FSharp-1.9.2.9\bin for me. F#: Word Count using a Dictionary. Having spent some time unsuccessfully trying to make my F# attempt at the word count problem work I decided to follow the lead of the other examples I’ve read and make use of a Dictionary to keep count of the words.

F#: Word Count using a Dictionary

I originally thought that I might be having a problem with the downloading of the files and storing of those strings in memory so I tried to change that bit of code to be lazily evaluated: That didn’t seem to make much difference though and it seemed like the StackOverflowException was happening on the ‘List.fold’ line: Building WCF services with F#, Part 1. For a while now, I’ve held the opinion that the “sweet spot” for functional languages on the JVM and CLR will be in the services space, since services and functions seem pretty similar to one another in spirit—a given input produces a given output, with (ideally) no shared state, high concurrency expectations, idempotent processing, and so on.

Building WCF services with F#, Part 1

This isn’t to say that a functional language is going to make a non-trivial service trivial, but I think it will make it simpler and more likely to scale better over time, particularly as the service gets more complicated. As part those explorations into the union of services and functional languages, I’ve been taking some of Michele Leroux Bustamente’s excellent labs from Learning WCF and flipping the services over to F#. The basic syntax of F# - classes, interfaces, and members – Wind. This entry is part of "The Basic Syntax of F#" series: Today’s blog entry covers the F# syntax for authoring classes, interfaces, and members. Together with the previous blog entry, this probably covers the most common 90% or so of the language. Again today I will intentionally err on the side of simplicity, at the expense of total accuracy. Defining F# types There are lots of ways to define new types and type names in F#, and most involve declarations with the "type" keyword.

Classes (sans interfaces or inheritance) Here is an example skeleton class definition whose only purpose is to demonstrate most of the language syntax involved in defining classes: I’ll discuss each part in turn; the commented numbers are to help call out specific bits in the prose that follows. A class definition usually starts like this: type SomeClass(constructor-args) = ... but there are lots of optional bits that can go before the ‘=’ when defining a class. What does this C# code look like in F#? If you have C# code and just try to transliterate it directly to F#, the F# code you wind up with is unlikely to be idiomatic or "good". Nevertheless, when you are new to a language (F#), it is sometimes useful to know how to transliterate from a well-known language (C#) for those cases where you just don’t know the idioms yet, but don’t want that to prevent you from making progress. So today’s blog entry takes the general form of "here’s some C# code, how do I write the same thing in F#".

It is intended to be used as a reference or as casual reading to discover some little-used or lesser-known F# syntax/operators/functions. Don’t use this blog post as a way to learn the language, instead use the many useful F# tutorials and samples on the web (start here), and just use this blog post to "fill in the gaps" if needed. Today’s blog entry covers only code that goes inside a method body, so you won’t find anything about declaring classes or using namespaces here.

Without further ado! Implementing a Shopping Cart in F# One persistent question that keeps coming up to me is how to merge functional programming techniques with object oriented techniques that many are used to. My usual reply is to talk about how functional programming affects your code, programming in the big, programming in the medium and programming in the small. John Conway's Game of Life in 32 lines of F# F# on the Xbox 360.

This article describes how to build an XNA Game Studio application using an F# library in such a way that it can be run on the Xbox 360. It applies to the current version of F#, 1.9.7.8 A method was first described in this blog by Jack Palevich. Most of his blog post still applies, except for step 2. In order for an .net assembly to be usable on the Xbox 360, it must be linked against dlls for the Xbox 360, which can be found in <Program Files directory>\Microsoft XNA\XNA Game Studio\v3.1\References\Xbox360. In particular, it must be linked against mscorlib.dll for the Xbox 360, which is the difficult part. By default, when you create an F# library in Visual Studio, it is linked against FSharp.Core.dll, which references mscorlib for the PC.

In order to avoid conflicts between versions of mscorlib, one must build an F# library targeted at the Compact Framework 2.0, which is what the XNA .net framework is built upon. Compiling F# code for the XBox 360. An RSS Dashboard in F#, part one. As you may know, I am quite active answering questions on forums like StackOverflow and hubfs. An RSS Dashboard in F#, part two (IObservables)