December, 2012

Reading input in F#

This article was originally published at tech.blinemedical.com

I’ve been playing with F# lately, much to the the chagrin of Sam, but I still think it’s fun as an excersize in thinking differently. I also find its terseness lets you prototype ideas quickly, encouraging you to experiment and tweak your code. However, I’m much more used to imperative programming, so when I started writing an F# program that needed user input I hit a small roadblock: how do I get input, validate, and ask for it again if I want to stay purely functional and leverage immutable values?

In an imperative language you might write something like this:

String path = null;
while(true)
{
    path = Console.ReadLine();
    if (File.Exists(path))
    {
        break;
    }

    Console.WriteLine("File doesn't exist");
} 

You can’t declare path inside the while loop or its loses its scope. If you need to use path outside of the while loop, … Read more

Debugging piped operations in F#

This article was originally published at tech.blinemedical.com

A little on the pipe operator

In F# you can create piped operations using the |> operator. This takes the output of the previous statement and funnels it as the input to the next statement. Using the pipe operator, a statement like this:

x |> f |> g |> h

Means having functions nested like this:

h(g(f(x))

So a piece of code like this:

let print item = Console.WriteLine(item.ToString)

let seqDebug =
        [0..1000]
                |> List.map (fun i -> i + 1)
                |> List.filter (fun i -> i < 5)
                |> List.head
                |> print

Decompiles into this (formatting added):

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal static Unit seqDebugu00407;

public static void mainu0040()
{
    Program.print(
        ListModule.Head(
            ListModule.Filter((FSharpFunc<int, bool>) new Program.seqDebugu004010(),
                ListModule.Map<int, int>((FSharpFunc<int, int>) new Program.seqDebugu00409u002D1(),
                    SeqModule.ToList(Operators.CreateSequence(
                        Operators.OperatorIntrinsics.RangeInt32(0, 1, 1000)))))));

    u0024Program.seqDebugu00407 = (Unit) null;
}

Which really boils down to:

seqDebug = Print(Head(Filter(Map(sequence))))

The F# syntax is nice because it … Read more

,

RESTful web endpoints on Netduino Plus

This article was originally published at tech.blinemedical.com

I have a Netduino plus at home and I love it. Not only can you use C# to write for it, but you get full visual studio integration including live breakpoints! I got the Netduino plus over the Netduino because the Netduino plus has a built in ethernet jack and ethernet stack support. This way I could access my microcontroller over the web if I wanted to (and who wouldn’t?).

But to expose your Netduino to the web you need to write a simple web server. Basically open a socket at port 80 and read/write requests out to it. The Netduino community is great at sharing code and I quickly found a nice web server by Jasper Schuurmans. His server code let you define RESTful routes like this

http://NetduinoIPAddress/targetFunction/arg1/arg2/...

Which was super cool. It even filtered out non-registered commands, allowing you to … Read more

,