In the Down the Oregon Trail with Functional C# presentation by Simon Painter, he discusses his experience implementing functional programming in C# while developing a game based on the classic Oregon Trail. He shares his approach to handling user input using functional programming concepts, such as discriminated unions and monads. Simon also discusses the challenges of implementing a purely functional system in C#, including limitations with recursion and the need to make compromises. He shares insights into the history of programming languages, specifically BASIC, and its impact on the development of the Oregon Trail game. Throughout the presentation, Simon provides code examples and explanations to help viewers understand the concepts and techniques he uses.
Down the Oregon trail with functional C#: A comprehensive overview
The umbra and penumbra of Functional Programming
Simon Painter used the analogy of shadows to describe the challenge of making C# purely functional. The “umbra” represents pure functional programming, while the “penumbra” signifies the necessary compromises. C# cannot fully replicate a pure functional language, but Painter’s goal was to maximize the functional aspects while minimizing these compromises.
A nostalgic journey: The Oregon Trail
Shifting gears, Simon introduced “The Oregon Trail,” a game many remember fondly from their youth. Originally developed in the early 1970s, the game has educational roots, teaching players about the historical westward journey in America. Simon shared the game’s history, its ties to the BASIC programming language, and the early days of educational computing.
BASIC: The language of the past
Simon took a deep dive into the origins of BASIC, a language created to be simple and accessible. Developed by John Kemeny and Thomas Kurtz at Dartmouth College, BASIC enabled beginners to write programs easily. Painter highlighted the language’s simplicity and limitations, such as its reliance on line numbers and lack of modern programming constructs like objects and classes.
Bringing functional concepts to C#
With the historical context established, Simon transitioned to the technical heart of his presentation: implementing functional concepts in C#. He tackled the challenge of user input from the console, using an interface for test-driven development and wrapping console behavior. Simon introduced discriminated unions, specifically a “maybe monad”, to handle different input states.
Handling user input and state
To manage user input in a functional style, Simon created a “user input” layer, which could be empty, text, integer, or error. This approach was more elegant than traditional type-checking methods. Simon also discussed using records in C# to handle state, which are passed by value rather than reference, aligning better with functional programming principles.
Iterative processes and recursion
One of the more intricate parts of Simon’s system was creating an “iterate until” function, which repeatedly asked for user input until a condition was met. This functional approach was a more complex but cleaner solution compared to traditional loops, despite the lack of tail-call optimization in C#.
Overcoming challenges and creating a functional interface
Simon acknowledged the compromises required to create a functional-like interface in C#. He explored various options, including intermediate language manipulation and referencing F#, ultimately settling on a functional interface that appears functional to the user.
Modernizing the Oregon Trail
Simon detailed how he updated the game’s inventory and state management systems using functional programming. He created recursive functions to handle user inputs and updated the game state using records and switches. The most complex part involved determining random events and their outcomes, which he implemented using a binomial equation and dice rolls.
Conclusion
Simon Painter’s presentation was a testament to the power and flexibility of functional programming, even in languages not inherently designed for it. By taking a nostalgic journey down “The Oregon Trail”, he demonstrated how functional principles can lead to cleaner, more maintainable code.