C# · Functional Programming · Hakell

Why functional programming?

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:

  1. 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.
  2. A declarative language simplifies the creation and multiple interpretations and static analysis.
  3. Compositional construction allows natural mapping of the problem domain and encourages code reuse.
  4. 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.
  5. 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.
  6. Its easy to implement domain-specific language.

Disadvantages:

  1. Its difficult to get developers on functional languages and thus it adds to project risk.
  2. UI development is not a proven ground in functional world.
  3. 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.

  1. type Put = String
  2. type Call = String
  3. data Option = Call | Put
  4. deriving (Eq, Show)
  5. blackscholes :: Option -> Double -> Double -> Double -> Double -> Double -> Double
  6. blackscholes  option s x t r v
  7. | option == Call = s * normcdf d1 – x*exp (-r*t) * normcdf d2
  8. | option == Put = x * exp (-r*t) * normcdf (-d2) – s * normcdf (-d1)
  9. where
  10. d1 = ( log(s/x) + (r+v*v/2)*t )/(v*sqrt t)
  11. d2 = d1 – v*sqrt t
  12. normcdf x
  13. | x < 0 = 1 – w
  14. | otherwise = w
  15. where
  16. w = 1.0 – 1.0 / sqrt (2.0 * pi) * exp(-l*l / 2.0) * poly k
  17. k = 1.0 / (1.0 + 0.2316419 * l)
  18. l = abs x
  19. poly = horner coeff
  20. coeff = [0.0,0.31938153,-0.356563782,1.781477937,-1.821255978,1.330274429]
  21. horner coeff base = foldr1 multAdd  coeff
  22. where
  23. multAdd x y = y*base + x
  1. namespace BackScholes
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. }
  8. }
  9. enum Option
  10. {
  11. Call,
  12. Put
  13. }
  14. /// <summary>
  15. /// Summary description for BlackSholes.
  16. /// </summary>
  17. public class BlackSholes
  18. {
  19. public double BlackScholes(Option option, double S, double X,
  20. double T, double r, double v)
  21. {
  22. double d1 = 0.0;
  23. double d2 = 0.0;
  24. double dBlackScholes = 0.0;
  25. d1 = (Math.Log(S / X) + (r + v * v / 2.0) * T) / (v * Math.Sqrt(T));
  26. d2 = d1 – v * Math.Sqrt(T);
  27. if (option == Option.Call)
  28. {
  29. dBlackScholes = S * CND(d1) – X * Math.Exp(-r * T) * CND(d2);
  30. }
  31. else if (option == Option.Put)
  32. {
  33. dBlackScholes = X * Math.Exp(-r * T) * CND(-d2) – S * CND(-d1);
  34. }
  35. return dBlackScholes;
  36. }
  37. public double CND(double X)
  38. {
  39. double L = 0.0;
  40. double K = 0.0;
  41. double dCND = 0.0;
  42. const double a1 = 0.31938153;
  43. const double a2 = -0.356563782;
  44. const double a3 = 1.781477937;
  45. const double a4 = -1.821255978;
  46. const double a5 = 1.330274429;
  47. L = Math.Abs(X);
  48. K = 1.0 / (1.0 + 0.2316419 * L);
  49. dCND = 1.0 – 1.0 / Math.Sqrt(2 * Convert.ToDouble(Math.PI.ToString())) *
  50. Math.Exp(-L * L / 2.0) * (a1 * K + a2 * K * K + a3 * Math.Pow(K, 3.0) +
  51. a4 * Math.Pow(K, 4.0) + a5 * Math.Pow(K, 5.0));
  52. if (X < 0)
  53. {
  54. return 1.0 – dCND;
  55. }
  56. else
  57. {
  58. return dCND;
  59. }
  60. }
  61. }
  62. }
Advertisements

2 thoughts on “Why functional programming?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s