background preloader

F#

Facebook Twitter

The Future of F#: Type Providers. The Combinator Approach to Programming Domain Specific Languages with F# Real world F#: my experience (part two) | Index Out Of Range. The second project I recently completed in F# is a completely different animal. While the first one is a pet project I’ve put together in my spare time (with no deadline at all), this one has been a full-time work for my company (for this reason I cannot disclose some details or share source code).

Additionally, time available was limited. Very limited. Like 2 weeks limited. That’s 10 working days plus a 2-days emergency buffer. A load simulator tool My company produces a high performance client-server platform that ships with our own proprietary database engine. As you can guess, hiring and coordinating hundreds of people to load the system the way you need is very impractical, if possible at all. The application I was going to build was meant for internal use but it was clear that something usable by non-über-geeks would have been nice to have at some point (for example to help sizing hardware for large customers). Writing your own DSL Here starts the amazement. Walking the tree. Blog Archive » Lightweight rules engine reimplemented in F# Using multiple F# source files, and a useful debugging technique. In some prior blog entries, I highlighted some features of the project system that enable you to use and organize multiple files as well as multiple projects within your solution.

In my next blog entries, I want to continue showing you some more features along these lines, but now with more of an eye towards pragmatics, including some advice on how to choose among different ways to organize your code. Multiple files in F#, and the implicit module Suppose I have a tiny F# source code file like this: let AnyAsString x = sprintf "%A" x AnyAsString [1;2;3] in a different file, I get The value or constructor ‘AnyAsString’ is not defined.

The reason is that, if you write code at the "top level" of a file without using a "namespace" or a "module" (and I’ll talk more about when/how to use namespaces and modules to organize your code in a future blog entry), then F# implicitly puts your code in a module whose name is the source code filename. Debug.AnyAsString [1;2;3] // Debug.fsmodule Debug. Type Classes Are The Secret Sauce. I came across a recent post on adding Ruby and C# operators to F# that sparked a few thoughts in my head. The post was good, but yet there were operators that already existed for some of the operations mentioned such as the defaultArg and ( @ ). But what really got me was thinking about type classes again due to the fact that extension operators aren’t currently supported in the language whereas extension events, properties, methods and statics are. I covered this in the past in regards to implementing an approximate equality check for my Functional Programming Unit Testing series, but I want to dive further into that subject a little more in this post.

Just What Are Type Classes? As you may note that I do cover a bit of Haskell on this blog, and I think for very good reason. When we talk about implementing functions, we have two main types of polymorphism, parametric and overloading. Prelude> :t map map :: (a -> b) -> [ a ] -> [ b ] Prelude> compare True False GT And the list goes on. Catamorphisms, part three. As promised in a previous blog entry, today let’s apply catamorphisms to a more real-world-ish example. Say we’re creating a new programming language, and we’re authoring tools for this language. A parser for the language will probably output an intermediate representation called an abstract syntax tree (AST), which other language tools then process. For example, an interpreter would walk the AST and "execute" the code that it represents, whereas a pretty-printer would walk the AST and convert it into a nicely displayed text string.

If you were coding in an OO language, you’d probably apply the visitor pattern, where the interpreter and pretty-printer were two different kinds of visitors accepted by the AST. (Once upon a time I gave just such an assignment in a programming languages class.) On the other hand, if you’re using a functional language, then each ‘visitor’ is just a catamorphism on the tree type. Today’s tiny language Discriminated unions fit the bill nicely. Lovely.

Summary. Vector, Matrix and Linear Algebra in F# The following script snippets present the vector and matrix functionalities in Microsoft.Fsharp.Numerics namespace from Fsharp.PowerPack assemble. Also, they present the Linear Algebra functionalities from FlyingFrog namespace from FSharpForNumeric assemble. //F# documentation at // //Fsharp.PowerPack documentation at // //Flying frogs numerics documentation at // #I @"C:\Program Files (x86)\FSharpPowerPack-2.0.0.0\bin" #r "Fsharp.PowerPack.dll" #r "FSharpForNumerics.dll" #r "FSharpForVisualization.dll" open Microsoft.FSharp.Math open FlyingFrog open FlyingFrog.Graphics open FlyingFrog.Graphics.Typeset;; //Simple operations 2.0*v1;; v1+v2;; v1-v2;; v1. //Vector Generic type intVec = Vector type intVec = Vector >val it : Vector // element-wise product f (A. type cmatrix = Matrix. Scripting in F# - Chris Smith's completely unique view.

The thing you hear most about F# is that it is multi-paradigm, meaning that you can use it to code in multiple styles of programming. But F# spans multiple-domains too. F# is not only well suited for quantitative computing, but it is surprisingly well suited for scripting as well. The use-case for scripting is that you want to automate some task but don’t feel like resorting to writing a full-blown application to do it for you. For example, on the test team we use Visual Studio Team Test edition to gather Code Coverage for the F# product. In order to get our bits ready for code coverage I need to do the following: Un-GAC the assembly Instrument it using a filter Disable strong name verification GAC it Delete NGEN images NGEN it [Run tests] Un-GAC the instrumented binary Delete NGEN images Those steps are tedious as-is, let alone when you want to repeat the process for 10 different binaries!

So what exactly is an F# script file? Windows Explorer Integration Extra perks of F# Scripts. Language Oriented Programming in F# - Chris Smith's completely unique view. Last Tuesday I gave a talk to the .NET Developers Association entitled Language Oriented Programming in F#. You can find a video of the presentation here*. This essay is the written version of that presentation, which unfortunately doesn’t translate to the web so well. In fact, I’m going to go ahead and apologize now for this crazy-long post. What is Language Oriented Programming Let me start by saying that Language Oriented Programing (LOP) is a nebulous term, like meta-programming. To understand what LOP is first you must understand the concept of a Domain Specific Language or DSL.

The main advantage of a DSL is that the code is always much simpler than its general purpose programming language counterpart. So what is LOP then? FsUnit allows you to write F# code, but using words and concepts in a different language. The rest of this essay will provide examples of LOP in F# and how using LOP results in simpler code that better expresses the problem at hand. Abstract Representation. Demo. F# Quotations Samples on CodePlex. Generating parameterized F# quotations. Traversing and transforming F# quotations: A guided tour.

There seems to be some lack of information about quotations – most is either short, a specific example, or is a bit outdated. The most comprehensive example is probably Tomáš Petříček’s Quotations Visualizer in the F# samples. His site is also one of the better resources about quotations that I’ve found. Another one with some recent activity is Alex Pedenko’s blog. Furthermore, even the otherwise excellent Expert F# book has little to say about it, and predates the major changes that were made in the September CTP. Here I’ll focus on the important general patterns of using quotations. Either way, this post represents some knowledge that took me a while to figure out on my own, piecing together scattered sources. I would like to point out that once you get over the learning curve, working with quotations is actually enjoyable. The basics There are three ways to obtain a quotation: let typedExpr = <@ 1 + 5 @> let typedExpr2 = <@ fun i -> i + 5 @> Sending these to FSI gives: Why?

Luckily, no.