January 5, 2012 8:37pm
Random F# Knowledge gained today

Today I started going through the tutorial for F# on www.tryfsharp.org. So far this tutorial is excellent and lets you quickly try F#. The site uses a silverlight plugin which allows you to run F# code in your browser without any IDE. You are up and running within seconds. So far I managed to go through the first section which covers the basics of F#. I will admit some of the constructs so far are nice, not needing to use type annotations because of type inference and the clean syntax of the language (not needing {}, ;, etc). The only complaint so far in the tutorial is with the variable names, they are short and hard to wrap my head around (especially in the currying examples). Anyways here are some random notes I took while going through the first section. Some of this stuff is sort of basic, however typing it up will hopefully make it stick.

  • let statements are not assignment statements. Each let statement creates a new association between a name and a value. In functional programming, such an association is called a binding.
  • In F# indentation matters when writing code there is no termination character such as a ; to terminate a line.  Also there is no {} characters used if/else blocks as in C#.
    C#
    if(x < 5){
        Console.WriteLine("< 5");
    }
    
    F#
    if x < 5
         printfn "< 5"
    
  • There is no void return type in F#, instead the return type would be unit.
  • tuple

    A tuple is similar to a struct in C#, you can define them with any number of named values, however they should be kept fairly small. You can access the values of a tuple by either fst (first value of a tuple) or snd (second value of a tuple)
    let computer = ("iMac", "i7")
    let name = fst computer
    let cpu =  snd computer
    
    Where name contains “iMac” and cpu contains “i7”
  • tuples can not be modified, if you need to alter the values contained in a tuple you need to create a mutable type.
  • Lists

    Lists must contain the same type of values. Each item in a list is separated by ;
    let languages = ["C#"; "F#"; "JavaScript"]
    
  • You can create lists of number with the the “..” operator. let oneToTen = [1 .. 10] will create a list of numbers 1-10, however if you supply two “..” you can specify the number to increment the value. let plusTen = [1 .. 10 .. 100] (e.g. 1, 11, 21, 31…91)
  • Lists are 0 based and if you need to access an element of a list you simply use the [] syntax, with the previous example let number = plusTen.[3] will bind 31 to number;
  • If you want to add an element to the start of a list use :: let newList = -10 :: plusTen
  • If you need to join two lists together use the “@” operator, let myList = plusTen @ newList, this should be used only when needed since it can be expensive depending on the size of the list. You cannot alter an element inside of the list natively, however you can write a function that would allow this to happen by copying the contents and creating a new list. A list is immutable
  • Operators

    F# has the typical arithmetic operators, boolean operators, and comparison operators. One notable operator that is unique to F# is the compare operator.
    let x = 1
    let y = 2
    let lessThan compare x y //returns -1
    
    let x = 1
    let y = 2
    let lessThan compare y x //returns 1
    
    let x = 1
    let y = 1
    let lessThan compare y x //returns 0
    
  • Implicit type conversations do not happen automatically.
    let sum = 29 + 3.5 // does not compile
    
    If you need to provide an explicit conversion for this addition to occur.
    let sum = float 29 + 3.5 //works
    
  • There are a few built in implicit conversions in F# between types.
    • sbyte converts to a signed byte
    • byte converts to an unsigned byte
    • int16 converts to a 16-bit signed integer
    • uint16 converts to a 16-bit unsigned integer
    • int or int32 converts to a 32 bit signed integer
    • uint or uint32 converts to a 32 bit unsigned integer)
    • int64 converts to a 64 bit signed integer
    • uint64 converts to a 64 bit unsigned integer
    • float converts to a double precision float
    • float32 converts to a single precision float
    • decimal converts to a decimal number
    • string converts to a string
    • char converts to a unicode character
  • F# is a statically typed language, and type mismatches will be uncovered by the compiler.
  • Type annotations are not required in F# like in C#. F# achieves this by using type interference to determine what that actual type is in the application. Because of this type interference it is possible that the compiler might choose the wrong type as a parameter or return value. This especially occurs when dealing with + and * calculations. If you are not sure you can easily add the type names to make sure your method has the proper signature of float instead of the potential inferred type of int.
  • Functions are first class objects and can be passed around and even used a parameters in another functions.
  • Being able to pass functions around is essential for predicates passed into List methods such as exists, filter, map, reduce, fold.
  • F# allows for function currying which allows you to create new functions based upon other functions.
    let add a b = a + b
    let addn n = add n
    let f = addn 7
    // What is printed by the following line of code?
    printfn "Value of f 3 = %A" (f 3)
    
    What happens here is that you call function f with a parameter 3 it calls addn 7, addn then calls add n which then unravels to 3 + 7. I think I follow this, but not sure if I can describe it completely just yet.
  • Namespaces and modules

    To use another namespace in your F# program you use open System (similar to C#’s using System;)

Items accomplished

  • Read wikipedia
  • ordered a book

January 4, 2012 7:11pm
Language for January F# (1/12)

Today I met with our little group that is joining me on the journey on learning twelve programming languages in twelve months in the year 2012. We collectively decided to learn F# as our head first dive into our challenge.

Why F#?

Well to be honest the decision was more out of convenience, we all are .net developers running Visual Studio 2010, have all the frameworks installed, and have the given tools needed to get started with little or no effort. Given we are already in January coming off a holiday this seemed like the logical choice.

What is F#?

I will not go into to much detail about F# since everything you might want to know is located on wikipedia. So far all I know is that F# is a language that supports three major programming styles. F# is functional programming, imperative programming, and object-oriented programming.

The next step

  • Read the wikipedia page
  • Find a good book to start learning F#, maybe this one.
  • Go through the tutorials on www.tryfsharp.org. (run F# in your browser with silverlight)
  • Decide on a project to build.

Until next time…

printfn "Hello World, from my first F# program"

January 3, 2012 7:50pm
Start of a new year

Well a new year has started and people make new year’s resolutions and this year I plan on learning 12 new languages/topics this year that are outside of my comfort zone which is primarily C# web development.

Why am I doing this?

I have been developing with VB/VB.NET and C# for the past 8 ½ years and it is a time for something to change up routine.  I love C# and .Net, but I am overdue to take a look at how other developers solve problems with all of these other languages I hear about on StackOverflow and twitter.  I am in my box and I am comfortable but being a developer and being comfortable is probably one of the worst things you can do.  It is time to shock my system and see what all the fuss is about with F#, why I would want to use Python, and many others.  I hope this will gain me some perspective on how .NET is awesome or “wow .NET is not all that it is cracked up to be for creating my web application”.

What languages will I learn? *rough draft

  • PHP
  • Java/JSP
  • Ruby on Rails
  • Perl/Mod Perl
  • F#
  • Python
  • Node.js
  • CppCMS (C++ Web Development Framework)
  • Objective-C
  • Haskell
  • Power Shell Scripting
  • Scheme

Now I know not all of these are languages, some are frameworks for a language.  Still the concept is the same, how on earth do you use C++ to build a web site?  If i want to use a Node.js binding to access a MySql database how on earth is that accomplished.

The Format

  • Create a specification that will be built in the language of the month
  • How to setup your machine
    • What do you need to install?
    • What do you use as an IDE?
    • Any configuration settings?
  • Was I able to create an application that fulfilled the requirement?
  • What libraries did I use if any?
  • How did I like it? Was it fun to develop in this language?

It begins

Well that is it for now, wish me luck and next time you hear from me I hopefully picked a language and what I plan on building.

Liked posts on Tumblr: More liked posts »