Last Friday we had a fantastic LearningByTeaching F# BootCamp in Leipzig. Each attendee got homework and had to solve one theoretical question and one programming task. For this two questions they had to present their results to the rest of us and after this I gave my solution in addition.
It was very interesting to see the different strategies and solutions. In this post series I will discuss the questions and some of the possible solutions.
Question 1 – What is “Functional Programming” in contrast to “Imperative Programming”?
This seems to be an easy question but in fact, the attendees had some problems to give a short definition of both functional and imperative Programming.
I didn’t find a formal definition of the terms so my intention was to clarify things with an informal description like the one from Wikipedia:
“In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in the lambda calculus, a formal system developed in the 1930s to investigate function definition, function application, and recursion.”
Wikipedia
I think the main aspect here is: avoiding state and mutable data. Maybe the words “side-effect”, recursion and “higher-order functions” could also be used, but they will be discussed in later questions.
On my slides I covered the following aspects:
- Functional programming is a paradigm
- FP tries to avoid shared state
- Functions are first class citizens, enabling higher-order functions
- Pure functions
- no side-effects
- Results calculated only on the basis of input values
- No information storage
- Deterministic
- ==> Debugging and testing benefits
- ==> Thread-safe without locking of data
For further reading I recommend "Conception, evolution, and application of functional programming languages" (Paul Hudak) or “Functional Programming For The Rest of Us” (Slava Akhmechet).
Question 2 – Explain the keyword “let”. In F# we are talking about “let-bindings” and not “variables”. Why?
Basically you use the let keyword to bind a name to a value or function. It won’t change any more, so a binding is immutable at default and not “variable”.
I was glad to see the presenter showing the problem with an imperative assignment like
x = x + 1, which from a mathematical view is paradoxical. There is no x which equals x plus one. I think choice of the F# assignment operator is better than equality sign. The statement x <- x + 1 shows the real intention. I want to put the old value of x plus one into the memory cell where x was before.
So we discussed some basic terms like scope and mutability here and I showed how we can explicitly tell the compiler to use mutable data using reference cells or mutable variables.
Maybe it wasn’t that good idea to discuss “Imperative F#” at such an early point (without knowing any functional concepts), but it showed the contrast to immutable let-Bindings.
Question 3 – What is a recursion? Try to explain why we often want recursions to be tail-recursive. Hint: Look at the following C# program. What is the problem and how could you solve it?
public static Int64 Factorial(Int64 x)
{
if (x == 0) return 1;
return x*Factorial(x - 1);
}
…
Factorial(10000);
It was interesting to see that nearly nobody expected a real problem in such a short code snippet. Some attendees thought this program might have an integer overflow – but only the presenters (they tested the program) gave the right answer (stack overflow). In fact they gave a very good and deep explanation about recursion and the problem on the stack.
As the question hinted, a possible solution was adding a accumulator variable and using tail-recursion:
public static BigInt FactorialTailRecursive(BigInt x, BigInt acc)
{
if (x == BigInt.Zero) return acc;
return FactorialTailRecursive(x - BigInt.One, x*acc);
}
Unfortunately this "trick" doesn’t work in C# (the compiler doesn’t use tail calls), but it leads to the correct idea – converting it to a while-loop. Of course I would prefer the tail-recursive F# solution:
/// Tail recursive version
let factorial x =
let rec tailRecursiveFactorial x acc =
match x with
| y when y = 0I -> acc
| _ -> tailRecursiveFactorial (x-1I) (acc*x)
tailRecursiveFactorial x 1I
We didn’t cover continuation passing here. I think this could be something for an advanced session.
Next time I will discuss the rest of the introduction and show some of the first programming tasks.
Tags:
F#,
factorial,
Functional Programming,
tail-recursion
Eben bin ich beim Lernen für die Datenbankenprüfung auf einen Wettbewerb mit dem merkwürdigen Namen “wettbehalle” gestoßen. Dabei soll für einen Vortrag meines Professors bei der Langen Nacht der Wissenschaften 2009 in Halle eine Seite möglichst gut für diesen Begriff bei Google gelistet sein.
Ich habe auch schon interessante / riskante Versuche im Netz dazu gesehen, allerdings glaube ich, dass diese konsequent abgestraft werden dürften.
Bei besonders umkämpften Suchbegriffen (die ich hier aus gutem Grund nicht aufzähle), wird jede Form der Manipulation erfahrungsgemäß von den Suchmaschinen-Betreibern schnell entdeckt. Bei einem so seltenen Wort wie wettbehalle, nach dem vermutlich nur zur Langen Nacht der Wissenschaften gesucht wird, bin ich mir jedoch nicht so sicher.
Möglicherweise (bzw. sehr wahrscheinlich) wird da auch etwas automatisiert unternommen. Fakt ist aber, dass der Wettbewerb nicht unter echten Marktbedingungen ausgetragen wird.
Obwohl es um Optimierung geht, werde ich für den Wettbewerb nur diesen einen Artikel hier und einen Tweet “einreichen”. Es wird zwar schwierig damit gegen eine Parteienseite und die Uni Halle zu bestehen, aber vielleicht wird ja zu starke Optimierung auch auf diesem Level schon abgestraft.
PS: Ok, das ging erstaunlich schnell – schon 9 Minuten nach dem Posten des Artikels hat Google ihn ganz oben gelistest. Wer will kann den Artikel aber trotzdem gerne verlinken.
PS 2: Vielleicht sollte ich noch etwas flamen – das hilft in der Regel immer um Links zu bekommen. Also bald findet ja das BootCamp zu “Funktionaler Programmierung mit F#” in Leipzig statt – in diesem Sinne: “Objektorientierte Programmierung wird völlig überschätzt.” 😉
Tags:
Lange Nacht der Wissenschaften,
wettbehalle
Seit Herbst 2008 bietet Microsoft TechNet eine neue Veranstaltungsreihe an: den sogenannten Microsoft TechDay. Im Mai wird sich diese Reihe um das Thema „Administrieren mit PowerShell“ drehen.
“Die Windows PowerShell, zeitweise auch bekannt unter den Codenamen Monad und Microsoft Command Shell (MSH), ist eine von Microsoft entwickelte Alternative zum Windows-Kommandozeilenprogramm cmd.exe und zum Windows Script Host.
Die auf dem .NET-Framework in der Version 2.0 basierende Windows PowerShell verbindet die aus Unix-Shells bekannte Philosophie von Pipes und Filtern mit dem Paradigma der objektorientierten Programmierung. Der Benutzer kann wie bisher einfache Befehle an einer Kommandozeile ausführen und miteinander verknüpfen oder aber auch komplexe Skript-Programme mit der eigens dafür entwickelten PowerShell Scripting Language schreiben.”
[Wikipedia]
Folgende Termine stehen zur Auswahl:
- 05.05.2009: Hamburg
- 06.05.2009: Düsseldorf
- 12.05.2009: Stuttgart
- 15.05.2009: München
Weitere Informationen zur Anmeldung und weiteren geplanten Themen findet man auf der Event-Seite des Microsoft TechDay bzw. auf der Agenda-Seite.
Im Juni wird es dann übrigens eine TechDays-Reihe zu Windows 7 geben. Man darf gespannt sein.
Tags:
microsoft techday,
powershell
"Implementing Microsoft Dynamics NAV 2009" is a new book by David Roys (MVP for Dynamics NAV) and Vjekoslav Babic (Dynamics NAV consultant). It shows the new features of Dynamics NAV 2009 in step-by-step explanations of real-world examples.
If you are interested in this book you can read the complete seventh chapter right here on navision-blog.de:
Chapter 6 (“Modifying the System”) is also available for download.
What the book covers
Chapter 1
The purpose of this chapter is a teaser introduction to get you excited about the product, what’s in it in general, and what’s in it as compared to previous versions, to give you a little taste of what’s coming up in the book, and explain what the fuss about this new release is all about.
Chapter 2
The RoleTailored client is the new user interface for users of Microsoft Dynamics NAV 2009, and it is completely different to the pervious versions. We’ll take you through the different components of the interface, introduce the terminology, explore the navigation components and page types, and teach you how to personalize the application to meet your own requirements using the extensive personalization features.
Chapter 3
Microsoft Dynamics NAV 2009 introduces a new paradigm to ERP. Instead of the system being focused on the forms that capture and present data and the functions the user can perform, the system is based around the individuals within an organization, their roles, and the tasks they perform. We cover how Microsoft researched the roles and explore the departments, roles, and tasks that have been identified in the Microsoft Dynamics Customer Model. We also show the reader how to assign the standard roles to users, how to create new roles, and how to allow departmental super users to configure the application for their role so that the change is applied to all users with the same profile.
Chapter 4
Microsoft Dynamics NAV is not a product with a Next-Next-Finish type of installation, and it takes a lengthy project to deploy it successfully. We focus on the six phases of the implementation process, and explain each phase with detailed dos and don’ts for a typical implementation. Based on the Dynamics Sure Step implementation methodology with advice liberally sprinkled throughout, special attention is given to new features of Microsoft Dynamics NAV 2009, and where the new capabilities must be taken into account to make most out of the implementation project.
Chapter 5
Every implementation of Microsoft Dynamics NAV 2009 will require the system to be configured to meet the needs of the business. This chapter tells the implementation consultant how to do this from a core financials perspective and provides valuable information that will allow developers to understand more about the application they are changing. We cover basic accounting for programmers, dimensions, and posting groups, and how to use the Rapid Implementation Methodology (RIM) Toolkit to speed things along.
Hardly any standard system can fit the needs of a business out of the box. Either the customer must shape their processes to match the system, or the consultant must shape the system to match the processes, and usually the latter prevails. This chapter explains the process of modifying the system, how to design a viable data model, and how to design and develop a functional user interface for both RoleTailored and Classic clients, without writing any code.
The three-tiered architecture of Microsoft Dynamics NAV 2009 and native Web Services Enablement open up a whole new world of possibilities for NAV implementations. We cover some of the many possibilities for extending the application, allowing the consultant and developer to understand the technologies that are available and their respective design considerations. Our practical examples introduce the NAV programmer to the world of .NET and show how you can use the information available on the internet to develop your own killer .NET add-ons.
Chapter 8
There’s much more to development than programming. It starts with understanding what customer really needs, and usually extends way beyond the system being deployed to a test environment. This chapter focuses on the development phase, and what it takes to get from a concept to a live and working solution.
Chapter 9
After the system goes live, or as it grows, there are periods when new problems may arise, and often their source is far from obvious. This chapter explores the tools and techniques available for detecting problems, pinpointing the source, and helping to remove them from the system quickly and painlessly. It explains how to debug the Service Tier, how to troubleshoot performance issues, what can be done to avoid problems, and how proper planning before design can help to get it right the first time.
Chapter 10
Our sample application focuses on requirements gathering, functional specification creation, solution design, and the eventual build of a prototype. We look at how a business problem can be explored using techniques such as interviewing, use-case modeling, and object-role modeling to create a solution design that can be molded into a working prototype.
If you want to get more information about the book visit: http://www.packtpub.com/implementing-microsoft-dynamics-nav-2009/book
Tags:
Dynamics NAV 2009,
Navision