Get started with functional programming and F#

Working with functional programming requires a shift in your thinking, but has benefits in productivity for programmer and maintainer alike

1 2 Page 2
Page 2 of 2

Fold, reduce, map, and iterate

You can operate on lists and arrays using a variety of methods. Some of the most common are iter, which lets you call a function on every element of a list or array; map, which enables you to apply a function to each element of a list or array and put all the results into a new list or array; fold, which applies a function to every element of a list or array and includes an accumulator variable to create an aggregate value; and reduce, which is like fold except that it operates on two elements at a time and passes the results on to successive elements.

The examples below are all for lists, but essentially the same code works for arrays.

//Print every element of a list 
let list1 = [1; 2; 3]
List.iter (fun x -> printfn "List.iter: element is %d" x) list1

//Add 1 to every element of a list and put the results
// in another list
let newList = (fun x -> x + 1) list1
printfn "%A" newList

//Take the average of a list of numbers
// by accumulating a sum and dividing by the number of elements
let averageList list = (List.fold
     (fun acc elem -> acc + float elem) 0.0 list / float list.Length)

//Take the sum of a list using reduce (no accumulator)
// Handle ArgumentException, thrown when the list is empty
let sumAList list =
           List.reduce (fun acc elem -> acc + elem) list
           | :? System.ArgumentException as exc -> 0

let resultSum = sumAList [2; 4; 10]
printfn "%d " resultSum //output is 16

These and many more list and array functions are documented in the Microsoft F# language reference.

As you can see by now, you can do functional programming in F# and still be productive. You may have an initial period where you try to write F# in an imperative style, since the operators to support that are included in the language. Once you grok the way elegant methods like iter, fold, map, and reduce apply functions to entire data structures to generate aggregates, you may find yourself using them in favor of writing for loops.

Yes, functional programming does require you to shift the way you think about programming, but you'll find that the resulting improvements in your productivity and the maintainability of your code are worth the effort.

Functional programming fanatics would argue that once you make the mental shift to writing in a functional style there's no reason to go back to object-oriented programming. Programming paradigms come and go, however, and there's no reason to think that functional programming will fully displace object-oriented programming any more than object-oriented programming completely displaced structured programming, or structured programming eliminated writing spaghetti code. As Ed Post wrote in 1982, "The determined Real Programmer can write Fortran programs in any language."

Right now, few legacy systems have been written in a functional style, which means that most maintainers will of necessity conform to the original coding style -- and unfortunately most coding is maintenance of existing systems. On the other hand, if you're lucky enough to develop your own project or contribute to a green-field project with other developers interested in the functional paradigm, then you'll likely reap the benefits of the functional style fairly quickly.

Who knows? You might even be able to read your own code when you come back to it in 10 years.

Additional resources:

Copyright © 2017 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2