A record of doing a new thing every day in 2011
Been a very busy week, with the Bing launch in France, so I’ve had to grab my opportunities for new things when I can.
On Wednesday, I followed the advice of @phammond to try out some new programming languages when I’m stuck for ideas, and had a play with F#. F# is a functional programming language, which makes it very interesting to play about with as it forces you to think about problems in a slightly different way. After installing the software and figuring out how to get the console running, I managed to write some “Hello World” code:
module BraveLocation.FSharpDemo
open System
let output = “Hello World!”
System.Console.WriteLine output
The line that took a little time was when loading the above code from a file in the F# console, you need your code to be part of a module.
Pressing on, next step was to write a very simple function that takes 2 integers and adds them:
let sum2 a b = a + b
let testSum2Values = sum2 3 4
System.Console.Write “3+4=”
System.Console.WriteLine testSum2Values
All pretty straightforward, but the rather sparse syntax takes a little time to get used to.
Finally something a little more interesting, with a recursive function that adds all numbers in a list of integers:
let rec sumList c = match c with
| h::tail -> (sumList tail) + h // A list has a value (h) and a tail
| [] -> 0 // If the list is empty, return 0 to end the recursion
let testSumList = sumList [1;2;3;4;5] // Define a list, and sum the values
System.Console.Write “1+2+3+4+5=”
System.Console.WriteLine testSumList
A few newbie things of note here:
It’s all very interesting, and compiling to .Net makes it possible to easily mix and match languages as appropriate. Will definitely investigate more if I ever get time