Influenced by the sheer beauty of F# and the practicality of FsUnit, I decided to approach the concept of F# readability and more domain-oriented programming. For that I decided to program a small snippet, which pushes some element onto a stack of elements. However, instead of going with the usual, such as calling a function. which would eventually read like
I opted for something more readable. Luckily, hjgher order F# functions, i.e. functions that take parameters, which are themselves functions, come to the rescue, As you may see, from the snippet below:
module Test let onto s = s type 'a Stack = 'a List let EmptyStack = List.empty let push a (ontostack:'a list -> 'a list) (st:'a list) = let pushx stack elem = elem::stack pushx (ontostack st) a let mutable myStack = EmptyStack myStack <- push 'L' onto myStack myStack <- push 'O' onto myStack myStack <- push 'O' onto myStack myStack <- push 'C' onto myStack printfn "%A" myStack printfn "press <any> key to exit" System.Console.ReadLine() |> ignore
A call sequence like push <Element> onto <Stack>
reads much more natural than the function call presented above, or a variant like:
elem |> push stack
Clearly, the stack example is trivial, but imagine cases like the ones covered by FsUnit or applications to scientific or any domain-specific computing. Instead of letting the technicality of the implementation language shine through, domain-specific concepts and abstractions can be implemented to such a degree that domain-savy experts my (unknowingly) use F# to solve complex business problems.
With that … read you later.