Functional programming in Python and C++ [closed] - c++

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
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.

Related

Beginner: Should I start High Level or Low Level? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am relatively new to programming and want to be able to make native C++ programs for Linux and Windows.
I am just wondering as a beginner should I first of all learn low level languages such as C and assembly in vim or should I just straight out start in an IDE with C++?
If you want to learn C++, learn C++. Learning C or assembly language first is not only a waste of time, but usually teaches relatively poor habits that you need to work at un-learning before you use C++ well.
That's not to say that knowing C or assembly language makes it impossible to use C++ well -- but each requires decidedly different mind-sets, so it creates extra work.
If your ultimate goal is to learn C++, it is not a prerequisite that you learn C first. You can, but you don't have to.
The bottom line is, do what you feel most comfortable doing.
C++ is not (really) a high-level programming language. You're still manually managing your memory and getting undefined environment-specific behavior whenever you make a minor programming mistake. Besides that, C++ is a very unfriendly language for new programmers because it is both overly complex and (in my opinion) horribly designed.
I recommend starting with an actual high-level language like Java, Python or C# in combination with a fancy IDE. Starting with C is also an option if you want to concentrate more on low-level aspects rather than general programming techniques and paradigms.
Also you don't have to learn vim if you want to start programming, a simple editor such as gedit or Notepad++ will also work perfectly fine. An IDE specifically designed for your language is probably the most comfortable, though.
EDIT: As Jerry Coffin has correctly pointed out, this advice isn't really helpful if your goal is to program C++ applications. Although I'd still recommend starting with an easier (high-level) language to obtain general programming skills before you start with C++. If C++ isn't absolutely neccessary for the thing you want to achieve, it also isn't a bad idea to reconsider whether you actually want to use that language.
If you want to learn C++, start with C++. You don't need to learn C first; it would actually be somewhat counterproductive, since you'd have to unlearn some stuff when you moved to C++.
C and C++ are different languages, with different goals and philosophies. A well-written C++ program will not look or behave much like a well-written C program.
Once you get comfortable with high-level C++ features, then you can start delving into the lower level details.
"If I have seen further it is by standing on the shoulders of giants."
-Sir Issac Newton
Do not reinvent the wheel.
start as high and abstracted from the core as you can, and only revisit the core when there is no other way to advance in your road.
Your question seems to me being more about learning programming.
Language choice may be secondary to learning the programing paradigms/concepts.
So if programming is your focus, then you may first learn object oriented programming (OOP) concepts, so that you don't have to "adopt" them in a way people coming from procedural approach often do. Then, if needed, you may dig into procedural way and some C idioms/tricks and low-level approaches.
OOP can be taught in C++ as in Java etc. does not matter on that stage.
Once your mindset is "oriented", then the actual programming will be more about using existing libraries (APIs), which in fact will require more learning than the language itself.
So my advice is to learn OOP concepts first, then review your future language preferences. Have fun!!
Javascript is the first language I learned and I feel lucky that it was. With it I was able to skip past alot of the intricacies and barriers of other languages like static typing, pointers, and compiling. With javascript, you don't even have to install anything, just go here and you can begin trying things out: http://jsfiddle.net/X4PpW/ .
After I had a strong grasp of Javascript, understanding the concept of using pointers and classes in C was easy for me. Another good language to start with would be Python.
Also, what do you intend on making? Not all desktop apps have to be written in C. In fact some new frameworks out there borrow ideas from web applications or even allow embedding HTML from websites into your app.
Games: Unity3d http://unity3d.com/ (Games written in this can be ported to mobile devices or the Web but requires a plugin)
General app with UI : QT http://qt.nokia.com/products/

What should my teacher talk about in my Advanced C++ class? [advice needed] [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
My teacher for Advanced C++ has opened the class up for students to suggest whatever topics we want. What are some good advanced C++ topics to know? We've already covered:
template metaprogramming
the STL (obviously)
introduction to the boost libraries
Please give reasons for suggestions as well.
1) Exception Safety + RAII. Because this is the part where I find C++ very different from other languages I know. It is easier to do exception handling in C++ if you understand the rules and why they are set that way they are, especially how to benefit from RAII when doing exception handling.
2) Introduction to C++0x. Because I can't wait any more the fourth edition of The C++ Programming Language ;) If you have the chance to learn some useful facilities you would be ready for the transition.
Concurrency. Most students don't cover this and it's a growing necessity in modern computing, as they get more CPUs.
Consequences of C++ design on C++ compilers
related: failure of the export keyword and why no one implements it
Custom allocators
placement new/delete and when you actually want to use them
Design of a C++ garbage collector
Also, if you only started out with C++ and did not come from pure C, it might be worth going in a low level direction rather than a high level direction:
Understanding the proper use of 'volatile'
linking C++ with other languages (ie calling java or fortran from C++ or vice versa)
performance analysis and tuning of code
I would say lambda, either in boost or in C++ 0x. this is not something people will find on their own most likely, but I think it allows to reduce code and maintenance significantly.
Plus changes the way you look at programming in certain way. moreover, it gives solid introduction to functional programming.
for example, you can sort collection very concisely using some weird requirement:
std:: sort(begin, end, lambda::_1 + lambda::_2 > 0);
I would also add template expressions. I am currently playing with them, they are powerful tool to produce very efficient code while maintaining very close resemblance to problem description. plus, I do not think any other language has similar facilities. http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Expression-template
Concurrency and thread management?
Some cases studies from GOTW
How much of template metaprogramming have you done? This deserves a full course, so if it ignited your imagination you might want to dig into that farther. Diving deep into template programming will get you a long long way into modern C++ programming.
Patterns, real application building, app architecture design, etc. Everything else (you mentioned boost libraries, STL, etc) can be easily discovered while self-educating, but good and rational design is much harder to learn.
It wasn't clear if this was covered in your existing knowledge, so a few "advanced basics" are noteworthy:
Multiple inheritance
Polymorphism
Operator Overloading
Advanced Streams, creating custom streams
Socket programming
and possibly GUI programming although most likely this is a separate course altogether.
C++ concepts, which if eventually adopted, will make it possible to typecheck templates and to get sensible error messages. You could study recent papers by Jeremy Siek and by Gabriel Dos Passos and Bjarne Stroustrup.
Creating a COW (Copy On Write) String class ?
Reflection and RTTI.

Learn C++ to understand examples in book fast, know C and Java already [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I need to read "A Practical Introduction to Data Structures and Algorithm Analysis" by Shaffer for class but the code examples in the book are all in C++ which I do not know. I know C and Java already and was wondering if you knew any resources that helped learn enough C++ to understand these examples fast if you already know another language. Thanks!
Another free textbook is The C++ Annotations by Frank B. Brokken. You can browse it online, or you can download the pdf version.
A quote from the first page:
This document is intended for
knowledgeable users of C (or any other
language using a C-like grammar, like
Perl or Java) who would like to know
more about, or make the transition to,
C++. This document is the main
textbook for Frank's C++ programming
courses, which are yearly organized at
the University of Groningen
What I like about "The C++ Annotations" is that is being kept uptodate, version 8.0.0 has
added C++0x chapters.
Yes, (the first edition of) Thinking in C++ taught me how to read C++ syntax; it was designed for C programmers and each chapter built incrementally onto my existing knowledge of C, which I found helpful (and it's now available in print or as a download). Many people recommend it.
I'm not familiar with the book, but any good Algorithms and Data Structure book should be understandable to anyone with programming experience, weather they understand the language the examples are in or not.
Especially, in your case where you already know Java and C, I can't see that you will have any problems following the algorithms just because they are written in C++
This is quite a textbook (and quite expensive, so see if your library has it), but I would recommend the man's book itself, the C++ Programming Language linky. I used it to enhance, rather than create, my understanding of C++, but I used it in the form of a dictionary, and it seemed to work out well. It is written for people comfortable with programming, and you've gotten the pointers AND the OO stuff down, so it may mesh well.
For example... Chapter 2, a Tour of C++ (as I have it open in my lap now), talks about a large number of things, many of which compare themselves to the "C" way of doing things. The things are not new to a Java programmer, but different syntax, etc. Basically, if you want to learn about C++ iterators, look up the iterators chapter, etc.
I think you'll do ok without necessarily needing a book, but C++ has ridiculously tricky syntax, (try figuring out how to assign a constant field in an object instance using the constructor, for example) and I found the book to be quite illuminating. Thankfully, there's a pretty nifty index including operators, which is quite helpful.
Lastly, if you want to be a guru (which I certainly am not), there are discussions about everything from "Exception-Safe implementation techniques" (Appendix E.3) to philosophy of developing large software projects.
So I've given you quite the advertisement (I've never met the guy, honest), but I've found the book to be quite useful.
See the following previous questions for online C++ learning resources:
https://stackoverflow.com/questions/45175/resources-online-to-learn-c
https://stackoverflow.com/questions/909323/what-are-good-online-resources-or-tutorials-to-learn-c
I'd recommend taking a look through C++ Primer Plus (5th Edition). What you probably need to get your head around is the syntax for:
the type system
templates
operator overloading
The basic syntax of C++ is usually fairly easy to get a hold of, however, C++ is a complex multi-paradigm language, which requires some serious study to use it effectively.
C++ Primer Plus (5th Edition) http://ecx.images-amazon.com/images/I/41YAKQF6BML._SL160_.jpg
I'm surprised no one has yet mentioned Accelerated C++. It's not based as much on your existing knowledge of C (or Java), but it will teach you the language as it stands alone.
I found 'C++' by Till Jeske, 2002, ISBN 0-201-75879-2 very good. It is not a pet killer, only 1.25 thick, and quite well written - concise and to the point. I knew some C++ before but Jeske's book really helped me.

Lisp as a Scripting Language in a C++ app [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Hey, I've been looking at the possibility of adding a scripting language into my framework and I heard about Lisp and thought I would give it a go. Is there a VM for Lisp like Lua and Python or am I in the wrong mindset. I found CLISP here, http://clisp.cons.org/, but am not sure if this is what I am looking for.
Can anyone point me in the right direction?
CLISP is just one implementation of Common Lisp. It's a very good implementation, and it does have some support for being embedded in other (C-based) programs, but that's not its focus and it's GPLed, which may or may not be a deal-breaker for you.
You might be interested in checking out ECL. This implementation is specifically designed to be embedded (indeed, the "E" stands for "Embeddable"!), and has numerous features that might be useful to you, including the ability to compile Common Lisp programs to C (as well as providing byte-code compilation and an interpreter).
Unless you need the whole of Lisp, you may want to settle rather on a Scheme implementation like Guile which is meant to be incorporated into another program.
Try Embeddable Common Lisp (ECL).
http://ecls.sourceforge.net/
It's targeted at embedding and you get only the parts of Common Lisp linked that your scripting language needs.
A Lisp is a good choice for an embedded language. Many people believe Lisp is hard but the syntax is relatively light, especially for non-programmers. There is essentially the prefix notation and that's it. Precedence rules are always unambiguous. Function names and variable names can be the same. You're pretty much free to use any characters you like for fun and var names.
With Lisp you can bend the syntax to your liking; the users do not have to learn common lisp. It is easy to extend and to provide, simpler facilities, such as expressing business rules or extracting data from files.
I guess my point is that the power and complexity of say Common Lisp, enables the provision of simple, domain specific constructs to the end user. Many other embedded languages will mean those users learning the intricacies of that language.
Chicken Scheme is another option for embedding. See here for details of the embeddable api.
There are a couple of easy options.
GUILE is the GNU extension language. It is an embeddable Scheme (dialect of LISP). GPL (naturally).
TinyScheme is a very small, very simple interpreter-based implementation of Scheme. It was successfully used by a malware company to do all kinds of nasty things. It is available in source form, I don't recall under what license(s).
Googling a little bit: Common Lisp as an Extension language
But keep in mind that Common Lisp wasn't designed from the ground up to be an extension language, unlike Lua or Guile.
A general advice: try to use an extension language that really makes the work of writing them easier, and remember that mastering Lisp so you can be really productive with it can take quite long (and there are not many people around that can stand so many parens xD).
Since it is not a Lisp, Fuzuli has a syntax similar to Lisp. It is easy to integrate it to C++ applications. The official site is http://www.fuzuliproject.org
Another one is newLISP at http://www.newlisp.org/ and it is also not a Lisp but very close to Lisp.
Lisp is a family of languages.
Common Lisp is an ANSI standard that is huge. Think C++ huge. Don't use it as a script language.
Unless you are targeting fairly hardcore programmers, Lisp as a scripting language is going to be...er....not well taking. Probably. Lua is likely a better bet as a script language.
That said, a Lisp is fine(technically) for implementing a scripting language.

Should I learn Python after C++? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I`m currently studying C++ and want to learn another language.
For work I use C# + ASP (just started learning it, actually), but I want something "less Microsoft" and powerful.
I have heard Python is a popular and powerful language, not so complicated as C++. But many people mentioned it was hard for them to get back to C++/Java from Python because they started thinking in it, get used to absence of memory management, etc.
What do you recommend?
There's no right or wrong answer, really. But I think you'll benefit more from learning Python. Given the similarities between C# and C++, you'll learn a different way of thinking from Python. The more ways you learn to think about a problem, the better it makes you as a programmer, regardless of the language.
The benefit of going from a more static language to a dynamic language is to change your programming paradigm -- it's not a matter of becoming "lazy" so much as realizing new ways of accomplishing things, which will make you better in any language.
Well, I've learnt Python after C/C++, Java and C#. Python is a great language, and its simplicity and consistency have improved the way I code. It has also helped me think more clearly about the algorithms underlying my code. I could go on about the benifits it brought me, instead I'll summarize the reason to learn it ->
Learning a new lanuage doesn't take away, it adds to your programming skill and keeps you sharp by teaching you to shift between the frames of mind that each language requires.
So go out there and learn Python. Your code will improve(TM).
P.S.
1.You'll lose C++ (or any other language) skills, if you neglect their upkeep and maintainance. Thats entirely up to you.
2.Programmer (intelligent) laziness is a virtue.
Many would argue that you would benefit from learning Python before C++.
The syntax hurdles are much, much lower;
Debugging is much more friendly
There are a plethora of libraries---batteries included, you know. It's easy to
experiment with web scraping, XML, etc. in Python. Again, the barriers to entry
in C++ are much higher.
It's still good to learn C/C++, because of its close connection to the machine. But a new programmer can learn an awful lot from exploring in Python.
I don't think that "Python makes you lazy" (nice title, anyway!).
On the contrary, in programming as in life, knowing more than one language is important; I think you'll find python amusing and sufficiently different from C++ or C# so that the languages will not get mixed in your head...
Python is complementary to C++ and easy to integrate with C++. (As evidence of this claim, the C++ gurus from Boost use Python.)
And as you said, Python gives you a way to get a perspective outside the Microsoft orbit. But even there, if you need to integrate Python with MS tools, there's IronPython.
Learning more languages can only make you a better developer, regardless of their approach. Besides, your experience with C++ (or, at least C) will come in handy for writing high-performance parts of your applications using Python's C API, which lets "raw" C and C++ code intermingle nicely with the pure Python stuff.
I still write code in Objective-C (1.0... before memory management) and Python on a daily basis. The variety is actually fun, rather than confusing; keeps things from being boring.
Flex your brain and improve your skill set. Give a functional language a whirl.
I learned C/C++, Java, Python & C# in that order.
The two I actually invariably end up using are C++ & Python; I find the niche Java & C# occupy between them to be too narrow to feel the need to use them much (at least for the stuff I do).
I also think I didn't really "get" C++ functors and boost::bind until I'd been exposed to Python.
Many languages are quite similar to others, but to move between imperitave and functional / dynamic and static / Object and Procedural languages you do need to train yourself to think within the constraints of the language you are using. Since most projects are at least a few weeks, this is generally not a problem after the first few days.
You will find it more difficult to switch away from a language+environment you enjoy in your after-hours / hobby development.
C, Macro Assembler => basically the same - difference is mainly libraries
C++, Java, C#, Delphi => basically the same paradigm - you learn quickly how to leverage the features of the specific language and adopt concepts from one syntax to another. It's basically the same way of thinking, the biggest exception is how you think of memory manangement.
Python - good language, strategically a better choice than ruby, although there are other aspects of ruby that can be argued to be superior. What make python a good choice is the presence of a formal language body which keeps python environments on different platforms very compatible to one another.
If you are interested, read this http://cmdematos.com/?p=120 on making a strategic language choice.
It is up to what exactly is the kind of applications you want to program, for example for Websites that need access to databases I would go for Ruby( and Ruby on Rails framework ) , for financial applications or applications that need a lot of parallel processing I would go for a funcional programming language like Haskell, oCaml or the new F#, these last 3 wil make you a better programer even if you don't programm a lot in them , by the way c# has been lately in the latest versions adding more and more funcional programming features. I would learn Python for a security and exploits kinds of applications.
You could learn a new programming language, like python, and use it to do all the tasks you'd normally perform in your 'core' languages; or you could take a language (like python, or perl) and use it to complement your core language.
You could learn VBScript and use it to write scripts that glue your code and others together. If you want something less Microsoft, then python, perl or bash scripting would be a good idea - not just to learn how to code in the new, but also how to do things differently from the usual 'code an app' way.
From a utility perspective, it is good to learn one of the more dynamic languages like Python (or Ruby or Perl) too. Not only do they stretch your mind, but they are superior for certain kinds of tasks. If you want to manipulate text, for example, C++ is a lot harder to use than Python. It gives you another arrow in your quiver to use when appropriate.
I learned, in order:
BASIC
Pascal
Ada
(A little bit of Haskell)
Java
Python
C++
C#
I don't feel Python inhibited my ability to learn or use C++. I am glad though that I learned pointers in Pascal before encountering reference types in Java, Python and C#, because I feel it gave me a good basis to understand the idea of the differences between "value types" and "reference types". I think for me the most important of those languages are Python, Haskell and C++. All of them complement each other, and although there are times I'm working in one and wish I had a feature from another, on the whole I think I benefit greatly from a deeper understanding of things like type systems, object orientation and metaprogramming by seeing the different ways these languages approach these things.
Try LISP instead (or afterwards, it's your call). You are at least partially right, though. using Python for a while makes you not want to go back to a statically typed and compiled language. It's just sooo much more comfortable not to have to please the compiler like ALL THE TIME ;). And yet another aspect is the readability of python code, which is awesome.
It is true. After learning python, everything else will seem like too much effort for the same amount of real work being done. You'll get used to the clean, small syntax and the freedom of GC. You will enjoy working in list comps, generators, etc. You'll start to think in python and C++ and Java will be like building a ship in a bottle one twiggy little stick at a time.
But since it's that much easier, doesn't it tempt you to try it all the more?
I think it is always good to know several programming languages. I've learned c++ at school and I've used it a lot in the past years because it is really a standard in the industry. I've learned python by my own and I am using it to make a lot of nice tools that would be too long to write in c++.
Python has just a very positive influence on my c++ skills. It gives another way to think.