Skip to content
fsharpforfunandprofit

fsharpforfunandprofit

This site shows you ways that F# can help in day-to-day development of mainstream commercial business software

fsharpforfunandprofit.com
fsharpforfunandprofitfsharpforfunandprofit
RETAIL SHOPPING AND CONSUMER AFFAIRS

Shopping cart example in C#

This is an appendix to the post on “designing for correctness”. In that post, I had some requirements for a simple shopping cart. I showed some bad C# code, and some F# code that implemented the requirements easily. There are some responses that come up when a C# developer looks at the F# code, such as “why not just use an interface?” or “what about the visitor pattern?” In this post, I’ll demonstrate why the solution is not as straightforward as you might think.
fsharpforfunandprofitfsharpforfunandprofit
VISUAL ARTS AND DESIGN

License

Text and images All text and images copyright (c) 2012-2021 Scott Wlaschin MIT License for code Code copyright (c) 2012-2021 Scott Wlaschin All code on this site is released under the free MIT License as follows: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
fsharpforfunandprofitfsharpforfunandprofit
SOFTWARE DEVELOPMENT AND APPLICATIONS

Introduction to the 'Why use F#' series

This series of posts will give you a guided tour through the main features of F# and then show you ways that F# can help you in your day-to-day development. Key benefits of F# compared with C# If you are already familiar with C# or Java, you might be wondering why it would be worth learning yet another language. F# has some major benefits which I have grouped under the following themes:
fsharpforfunandprofitfsharpforfunandprofit
BOOKS LITERATURE AND LANGUAGE

F# syntax in 60 seconds

Here is a very quick overview on how to read F# code for newcomers unfamiliar with the syntax. It is obviously not very detailed but should be enough so that you can read and get the gist of the upcoming examples in this series. Don’t worry if you don’t understand all of it, as I will give more detailed explanations when we get to the actual code examples. The two major differences between F# syntax and a standard C-like syntax are:
fsharpforfunandprofitfsharpforfunandprofit
SOFTWARE DEVELOPMENT AND APPLICATIONS

Comparing F# with C#: A simple sum

To see what some real F# code looks like, let’s start with a simple problem: “sum the squares from 1 to N”. We’ll compare an F# implementation with a C# implementation. First, the F# code: // define the square function let square x = x * x // define the sumOfSquares function let sumOfSquares n = [1..n] |> List.map square |> List.sum // try it sumOfSquares 100 The mysterious looking |> is called the pipe operator.