Is there any good book for functional programming in Python or C++ ? I need to master functional programming in those languages.
By functional programming, I assume you mean referential transparency (basically, no global state or side-effects), plus things like functions as first-class objects, polymorphic types, partial function application etc.
There is no book that I know of that covers functional programming in C++. You could program without global state and side-effects in C++, and you can pass functions as arguments using function-typed pointers, but you couldn't get partial function application, nor anonymous lambda expressions.
Text Processing in Python uses a functional style, and is what turned me on to functional programming. It's also a great Python/programming book in general, and I highly recommend it.
MMhh if you want to learn functional programming you should learn a functional language first to really understand the principals then you can tree to apply them as good as you can, witch will be harder.
In python you can use functions to modify dictionarys witch is pretty functional. Make use of lambda with higher order function. You have to avoid classes and inheritance.
I cant really say much about C++. Maybe you can find some functional datastructures and then write functions on those. Look for a library that provides functions like map, reduce ...
C++0x should support closures and currying (more or less) so thing will get better.
In general:
- Try to write immutible layers on librarys (be awair that that wont performe to well)
- Look for librarys that are written in a functional way
Edit: I would recomend learning scheme its really small and you can pick it up fast. Read something like SICP or the Little Schemer that teach you recursiv thinking.
Only a future version of C++ supports lambdas / anonymous functions. I guess Boost.Lambda supports C++ Functional programming, but it isn't really a first-class citizen of C++ yet.
Books about functional programming typically use functional languages. Like Haskell, Miranda, Lisp, Scheme, OCaml, Javascript and so forth.
EDIT: I'll withhold my opinion on Python for now. It appears I'm mistaken about a few things.
In looking for info on functional programming in Python, I've found this web page to be very useful:
http://www.amk.ca/python/writing/functional
The references part contains a lot of crude information.
If you are looking for a book covering only functional programming in Python, I don't know about it.
C++ is more difficult. It has many of the ingredients (and it is steadily gaining more) for functional programming, but lacks many others. Actually, C++ was never designed for supporting functional programming, so it is typical that you'll be able to work with functional programming in some containers, but need to revert to imperative programming frequently.
While not a book, here is a site that can at least get you started on some things. http://www.ibm.com/developerworks/library/l-prog.html
As far as really understanding functional programming I would suggest something like "The Little Schemer" to get a quick handle on scheme. You can then apply the ideas to python.
For Perl, I can recommend "Higher Order Perl".
Don't know about Python or C++ though.
精彩评论