Thursday, October 18, 2012

Similarities between F# and Erlang

Funnily enough my main goal at the moment is to become a good functional programmer. I have been in the OO world since I was a baby =D.

To that end, this will be my first living post, I aim to list all the parallels between F# and Erlang. There are many parallels between them and the truth is that F# is a hybrid language as it supports objects (oh no). But whilst that is helpful when dealing with the .NET Framework it can lead to working in a completely OO manner in a supposedly functional language. Also in order to easily move back and forward between two very good platforms I believe this table is key in maintaining patterns that can be used across both platforms.

A short example would be the need to perform tail recursion on a list. In F# there are a number of methods using while/until/do and in erlang I find a tail recursion is normally required. To that end when I reach a need to iterate I try to implement a tail recursion so that when I'm writing programs I solve them the same way regardless of language and platform (minding the minor syntax differences of course). So here is my living list which I will update as regularly as possible:

None of these examples have been compiled. Variable names like String might need to be changed before compiling any of the code.








Tuples:



Erlang: {A,B} = {1, 2}

F#: (A, B) = (1, 2)


Notes: In erlang lower case variable names are atoms, in a tuple this is similar to an object type for example {person, Name, Phone, Address} would be a typical example of a "Person" object. F# does not support atoms and an additional variable maybe needed to store the object type (as string maybe) =(. Both languages support pattern matching.


Static List:


Erlang: [1,2,3]

F#: [1;2;3]

Notes: The structure is almost the same, minor different in the seperator's syntax. The pattern matching and recursion works the same for both instances.


Strings:


Erlang: recursion(String) ->
   case String of
      [head | tail] -> io:fwrite("~w",[head]),
      recursion[tail];
      [] -> done;
      _ -> fail
   end.

F#:
let rec recursion(String) ->
   match String with
      | String -> Console.WriteLine(String.sub s 0 1)
            recursion(String.sub s 1 (String.length s))
      | "" -> "done"
      | _ -> "fail"
    
Notes: I believe Erlang has a big positive in this part of the comparison. Erlang treats strings as a List of ints and conceptually this makes learning programming make a bit more sense and you are using list functions all the time. So to manipulate a string is simply a list manipulation and you can use the list manipulation functions. In F# strings as treated similar to C#/VB .NET where they're are string specific functions in the framework. The above is a good example of the difference in iterating through strings although the F# recursion is a best match equivalent but their are probably built in functions that will allow you to perform the changes you want with less code.


Tail Recursion:


Erlang:
recursion(List) ->
   case List of
      [head | tail] -> recursion[tail];
      [] -> done;
      _ -> fail
   end.

F#:
let rec recursion(List) ->
   match List with
      | [head :: tail] -> recursion(tail)
      | [] -> "done"
      | _ -> "fail"
    

Notes: The ease of recursion is a big plus to functional programming. F# has almost no terminators so the end of each block is harder to visualize although this is by design for "code resuability".  In F#, "members" are automatically recursive and don't need the "rec" prefix keyword.


Funs (Anonymous Functions):


Erlang: fun (x) -> x + 1 end

F#: fun (x) -> x + 1


Notes: Both are very similar but F# doesn't use a terminator and relies on indentation to determine where the fun is finished. They both support currying functions and can be quite useful with many of the built-in functions that take a fun as an argument.

No comments:

Post a Comment