Today was the day that I wrote a 'real' -- that is a non-trivial, example-from-the-book -- program. It was an exhilarating experience.
The program (actually three functions) computes the price of a stream of payments.
I was inspired by a task at the office: review code that computes the price of the stream of payments. The code was in two forms: Java and Fortran. The code also came with the math for the calculations, and I immediately thought of functional programming.
Today I set aside some time and dug out the one book on Haskell in my library. It had enough to get me started. With the book and the math, I was on a programming adventure!
Programming in Haskell is a trip. Just as object-oriented programming was alien to my brain trained in procedural programming, functional programming is alien to my brain now trained in object-oriented programming. I struggled a bit with the syntax and with the design of the program, and emerged victorious! I had a set of functions that performed the desired calculations. (I checked them with math calculated in a spreadsheet.)
The Haskell version is shorter and cleaner than the procedural versions. (The Fortran version is obviously procedural, and the Java version makes no use of object-oriented constructs.) The procedural version uses a loop and a temporary variable that is assigned at the end of the loop so that it can be used the *next* time through the loop.
The Haskell version has no loops and no assign-at-the-end distraction. If you know the syntax, it is straightforward and easy to understand. I believe it to be less efficient than the procedural versions (efficient meaning 'less CPU time') so with Haskell I have gained programmer time at the expense of CPU time, a trade-off that I am willing to accept.
I will say that I don't fully understand the Haskell version. That is, I am not fluent in the syntax. I have internalized the syntax of Fortran, C, Java, and other languages so I can look at the code and know what it does without thinking. I am still in the "think about the code" stage with Haskell. (And that's OK. I expect a ramp-up time with a new language.)
Showing posts with label functional programming. Show all posts
Showing posts with label functional programming. Show all posts
Saturday, April 23, 2011
Wednesday, January 12, 2011
Thinking in a recursive mood
A colleague issued me a lunch-time challenge today: Write a program to detect palindromes. After a few minutes of thought, I wrote a small program that used a single function to detect palindromes. It is a very functional-programming design, looking like this:
func bool IsPalindrome(s)
return true if (s.length < 2)
return IsPalindrome(s.substring(1, s.length - 1) if s[0] == s[-1]
return false
end
No looping! All objects are immutable!
What surprised me is that I thought of this solution quickly, and without thinking specifically about functional programming. It came to me, rather than me sitting down and deliberately looking for a function-programming solution.
func bool IsPalindrome(s)
return true if (s.length < 2)
return IsPalindrome(s.substring(1, s.length - 1) if s[0] == s[-1]
return false
end
No looping! All objects are immutable!
What surprised me is that I thought of this solution quickly, and without thinking specifically about functional programming. It came to me, rather than me sitting down and deliberately looking for a function-programming solution.
Subscribe to:
Posts (Atom)