Wednesday, May 7, 2014

Functional programming F # Tail Element

Write a function that given a list , return the last element in the list (assume the list always contain at least one element.

let rec tailElement (L) =     // passing L array list
match L with                     // match the element in the list
| [E] -> E                          // base case , if there is one thing in the list return that element
| _ -> tailElement(L.Tail)
 // if there are more than one element walk from the second element(L.Tail) of the list  at the end of the list we will get the last element


No comments:

Post a Comment