I tried the changes this evening, and they do indeed work. We've simplified the program and made the logic clearer. I also expanded the program to handle a list of payment values, and not assume that each payment is the same value. It was quite easy to do -- much easier than similar changes to a C++ or C# program.
Showing posts with label haskell. Show all posts
Showing posts with label haskell. Show all posts
Monday, April 25, 2011
Haskell, improved
I showed my Haskell program to a co-worker today. As we were looking at it, we noticed some possible improvements. (Surprising, since neither of us are Haskell programmers.)
Saturday, April 23, 2011
Haskell for real
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.)
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.)
Subscribe to:
Posts (Atom)