Tag / csharp

I would love to do more (or any) professional Haskell and I am really happy that I can do F# in my day to day work but the sad fact is: most of you probably may not (or don’t want to) use F# in your day-job and do C#.

Now that does not mean that you cannot program in a functional fashion – not at all. It only means that you might have to consider carefully if the different paradigm (and maybe the additional fight with C#s inferior type-inference and lacking syntax caps) will hinder you too much.

With this I want to start a small series where I give some examples of how the same small problem can be solved in a functional way in my favorite languages.

I hope some of you will sent me some proposals for future topics.

If not I will tease you with my usual non-real-life examples.

today’s problem: the Caesar cipher

As a first and easy example I want to show how a implementation of the infamous Caesar cipher could look like in Haskell, F# and finally C#.

For those of you who don’t know this algorithm: it’s a very easy encoding scheme (that you should never use in more than toy examples and games for kids) that Caesar used to hide messages from enemies.

You basically shift your alphabet by a constant (the key) to switch from plaintext to ciphertext:

The decoding is the same only that you shift in the other direction.

In programming you typically shift using addition modulo the max. representable number/character to get a bijection (the inverse here is the same function with the negative key).

Implementations

Just pick the once you (can) understand and ignore the rest – I’m sure you get it 😉

Please not that right now we cannot easily use constructors as functions and with |> so I introduced makeString (something you will see again shortly) – aside from this it’s more or less a typical translation from Haskell into F#.

Now of course this is a static class and stuff and your OOP heart might want you to transform this into some singleton-pattern or other OO-shit but please don’t 😉

Is it really that hard to pass the key parameter around? I don’t think so.

Disagree?

Leave a comment 😉

PS

Mathias Brandewinder suggested this Test Run article via twitter as a further example – I have to look at it more carefully (it’s been quite some time I did something like this) but I like the idea – maybe we can compare after – it might take some time though, but easter vacation is comming (even if it looks more like winter at the moment)

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

disable

If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.