This semester I’m taking a class on programming languages (aptly named “Programming Languages”). As any CompSci major will tell you, it’s either extremely interesting, or extremely confusing.
The first language we’re dealing with is F#, Microsoft’s (now) in house functional programming language, for use with the .NET framework. This is my first entry into the world of functional programming and I find it. . . interesting. At first, switching mind sets from imperative to functional was extremely hard. My first program (to calculate standard deviations of lists) was 8 functions using mutable variables (though I avoided using for loops). After we talked more about the language and how functional programming is supposed to work in class, I was able to condense it down to a few lines in 1 function in a truly function fashion.
On the whole, I can understand why functional programming is powerful. I like that I can right quick sort in 4 lines of code (5 including the function declaration statement). Being able to make powerful recursive methods in small and efficient methods is invaluable. However, I wouldn’t want to ever try doing this kind of programming for the whole of projects. It may simply be because I “grew up” on imperative programming in Java, C, and C#. The language makes the most sense to me as a supplement in the .NET framework, writing methods in F# and calling them from a C# program (When we talked about this idea in class, I was probably a bit more excited that I should have been about the prospect). I may see the light at some point, but for now I’ll enjoy my tenure in the world of imperative programming.
Just a couple things we’ve done in F# so far. The first is quick sort, and the second generates lists of random integers.
Quicksort:
let rec quick_sort list =
match list with
|[] -> []
|pivot::tail ->
quick_sort (List.filter(fun x -> x < pivot) list)@ [pivot] @ quick_sort(List.filter(fun x -> x > pivot) list)
#light
open System
let ran = System.Random()
let rec do_gen n cur_length in_list =
if(cur_length < n) then
do_gen n (cur_length + 1) (ran.Next() :: in_list)
else
in_list
let gen_list n =
do_gen n 0 []
let rec printfnList list =
match list with
| [] -> printfn ""
| head::tail ->
printf "%A; " head
printfnList tail
printfn "Enter a number: "
let in_length = Int32.Parse(Console.ReadLine())
printfnList (gen_list in_length)
let ignore = Console.ReadKey true
“The best way to predict the future is to implement it.”
– David Heinemeier Hansson

Leave a comment
Comments feed for this article