Exercise 4: ListsThis exercise will demonstrate how F# works with lists. It will cover how F#’s implementation of a linked list is very different from its counterpart in imperative programming languages, explain the Head + Tail approach and demonstrate the abilities this approach gives your code through the use of recursive functions and pattern matching. Task 1 – Creating ListsIn this task you will learn the syntax to create lists in F#.
Note:
F#, like all functional languages, implements its lists as linked lists. However, the implementation of linked lists in F# is a little different from that of imperative languages. Most imperative languages implement linked lists by creating nodes which contain a value and a pointer to the next node in the list. This makes it very easy to add items in the middle of the list. F# implements lists as Heads and Tails. Essentially, a list item node in F# consists of a value and a tail, which itself is another list.
For more information on how F# deals with lists, please refer to Dustin Campbell’s blog at https://diditwith.net/2008/03/03/WhyILoveFListsTheBasics.aspx
Next Step |