Functional programming is not a silver bullet, but learning it will indeed add to the knowledge of every software engineer about solving problem in different a way. One can always implement functional style of programming in any imperative language they use.

Advantages:

- For analysis of mathematical problem that requires human analysts, it is possible to represent the problem in a concise mathematical form that analysts can understand.
- A declarative language simplifies the creation and multiple interpretations and static analysis.
- Compositional construction allows natural mapping of the problem domain and encourages code reuse.
- Functional programming language like haskell makes maintenance easy due to its high level of abstraction which also makes addition of new features a lot simpler then it would be in any imperative language.
- As most of the data types in functional languages are immutable concurrency and parallelism is integral art of the language. Most modern functional languages like Haskell and Erlang support multi-core without any extra libraries.
- Its easy to implement domain-specific language.

Disadvantages:

- Its difficult to get developers on functional languages and thus it adds to project risk.
- UI development is not a proven ground in functional world.
- Performance is real unknown for most functional languages as its not widely accepted in developing of large real time systems. Haskell performance. (i personally believe that with multi-core systems any problem needs to be parallelized and immutability is the only answer to that type problems. So even imperative languages have to make data structures immutable to solve the problem on multi-core.)

Here is the implementation of Black Scholes model for European options in Haskell and C#. I wrote this example to show the difference between functional and imperative language, see it your self.

- type Put = String
- type Call = String
- data Option = Call | Put
- deriving (Eq, Show)
- blackscholes :: Option -> Double -> Double -> Double -> Double -> Double -> Double
- blackscholes option s x t r v
- | option == Call = s * normcdf d1 – x*exp (-r*t) * normcdf d2
- | option == Put = x * exp (-r*t) * normcdf (-d2) – s * normcdf (-d1)
- where
- d1 = ( log(s/x) + (r+v*v/2)*t )/(v*sqrt t)
- d2 = d1 – v*sqrt t
- normcdf x
- | x < 0 = 1 – w
- | otherwise = w
- where
- w = 1.0 – 1.0 / sqrt (2.0 * pi) * exp(-l*l / 2.0) * poly k
- k = 1.0 / (1.0 + 0.2316419 * l)
- l = abs x
- poly = horner coeff
- coeff = [0.0,0.31938153,-0.356563782,1.781477937,-1.821255978,1.330274429]
- horner coeff base = foldr1 multAdd coeff
- where
- multAdd x y = y*base + x

- namespace BackScholes
- {
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- enum Option
- {
- Call,
- Put
- }
- /// <summary>
- /// Summary description for BlackSholes.
- /// </summary>
- public class BlackSholes
- {
- public double BlackScholes(Option option, double S, double X,
- double T, double r, double v)
- {
- double d1 = 0.0;
- double d2 = 0.0;
- double dBlackScholes = 0.0;
- d1 = (Math.Log(S / X) + (r + v * v / 2.0) * T) / (v * Math.Sqrt(T));
- d2 = d1 – v * Math.Sqrt(T);
- if (option == Option.Call)
- {
- dBlackScholes = S * CND(d1) – X * Math.Exp(-r * T) * CND(d2);
- }
- else if (option == Option.Put)
- {
- dBlackScholes = X * Math.Exp(-r * T) * CND(-d2) – S * CND(-d1);
- }
- return dBlackScholes;
- }
- public double CND(double X)
- {
- double L = 0.0;
- double K = 0.0;
- double dCND = 0.0;
- const double a1 = 0.31938153;
- const double a2 = -0.356563782;
- const double a3 = 1.781477937;
- const double a4 = -1.821255978;
- const double a5 = 1.330274429;
- L = Math.Abs(X);
- K = 1.0 / (1.0 + 0.2316419 * L);
- dCND = 1.0 – 1.0 / Math.Sqrt(2 * Convert.ToDouble(Math.PI.ToString())) *
- Math.Exp(-L * L / 2.0) * (a1 * K + a2 * K * K + a3 * Math.Pow(K, 3.0) +
- a4 * Math.Pow(K, 4.0) + a5 * Math.Pow(K, 5.0));
- if (X < 0)
- {
- return 1.0 – dCND;
- }
- else
- {
- return dCND;
- }
- }
- }
- }

Advertisements

Hey, great blog…but I don’t understand how to add your site in my rss reader. Can you Help me, please 🙂 🙂

Awesome blog!

I thought about starting my own blog too but I’m just too lazy so, I guess I‘ll just have to keep checking yours out.

LOL,