Imperative computation in F# (I.) - Returning results from a function
One of the limitations of F# is that it doesn't very well support some of the
advanced imperative language constructs such as
or imperative style of returning value from a function, meaning that you can't write
return false in the middle of the function. This has
good reasons. F# doesn't in principle have the notion of currently executing statement
and instead treat every code you write as an expression. Clearly, when there is no
current statement, we cannot jump to other statements. If you're looking
for more information about these basic principles, you can take a look at my book
Real World Functional
Programming, which covers this distinction in details in chapter 2, but we'll look
at a brief example that will clarify this idea shortly.
Often, there is really no need to use
break or other imperative constructs
in F#, because you can write the same thing more elegantly using one of the provided higher
order function such as
there are still some cases where the imperative programming style makes it easier
to express our original intention. Also, implementing your own higher order
functions (akin to
Seq.exists) would sometimes be much easier if we
could just use imperative return.
So, what can be done about this?
Real World Functional Programming: Second review finished!
I've been working on the Real World Functional Programming in .NET book for quite some time now. In fact, I had the first discussions with Michael Stephens from Manning in March last year and I started thinking about the book at that time, so it has been occupying my mind for almost a year now! Until recently, I was feeling that we're not getting much closer to the end of this project, because writing a book is just a lot of work. However, I think I can finally see that we're getting closer to actually finishing the book. At Manning, we've recently finished the second review, which means that I've just got another set of very useful comments - a big thanks to all the reviewers! I'm also getting close to finishing the first draft of the whole manuscript (depending on the reviews, the content may still change a bit, but I expect to write at most one new chapter from now). Hopefully, the drafts will soon make it to the MEAP release of the book.