r/learnprogramming • u/sw1sh • Jun 09 '12
Types of programming
So i have been teaching myself Java programming for the last two months,and I understand that it's an Object-Oriented Programming language. But from my time of stalking these forums I've read a lot about functional programming,and other types that I don't really understand. I get that I shouldn't expect to know much outside Java after only 2 months,but I'm just interested in how other languages differ from Java.
I've also read about Haskel,Scala and other seemingly unusual languages,and so my question is:
TLDR - "What are the differences between the programming types?"
215
Upvotes
102
u/kqr Jun 10 '12
Great! It's a very exciting world with lots of things going on independently, and if you manage to view it all in an unbiased fashion, you can learn a lot.
Yes, It's really, really weird and very difficult. I have a friend who, too, have toyed a little with Prolog, and he likes to say the experience is like "programming... only backwards."
It's not uncommon for me, when playing around with Prolog, to write some rules for the solution that feels natural to me, and then just blindly test my program to see what I've accomplished, and then it works. And I have no idea how it does.
Learning Java well first is a very wise decision and I'm glad you made it. I was thinking about including a paragraph where I tell you to be careful with jumping between languages without learning the one you're doing properly first, but it seems you have realised that by yourself.
Haskell is the playground for most of the programming language research today, so it's really interesting in that sense, yes. And people who use Haskell likes to use fancy words as well (what is a functor? What is a formal category? What is a monad? What does it mean for a program to have partiality?) so you'll pick up lots of those in time as well.
That is unfortunately not something I can easily explain. It's just that you get a particular set of functions and data types and whatnot built into the language, but then you can implement your own, and your implementations extend the language. When you have implemented your own extensions to the language, using them feels just like they were in the language from the beginning.
The particular example I have is much too complicated to be easy to get (I spent a good two hours trying to understand, probably), but the thing is that what you do might be really complex and difficult and require lots of code, but once you've done a proper implementation, using it is a breeze.
In that particular example, the author had a function called
accumSumwhich is meant to read in an infinite list of numbers and output an infinite list of numbers which are the sum of the numbers read so far. As an example, ifaccumSumwould be applied to the infinite list of all the positive odd numbers:It would output the infinite list of the accumulated sums, like so:
At each step, all the previous elements are summed and outputted. I keep saying that the lists are infinite because they really are. In Haskell, they can be.
Anyway, the function for doing this accumulated sum is, to start with, not very complicated, but kind of long:
(No, I don't expect you to understand a thing. Just look at the length of it, and realise that it's a very specific function for just this purpose.)
Then the author goes on to realise that
accumSumactually follows a more general scan pattern, so he defines ascanfunction.The
scanfunction is very general, very academic in its nature and very, very difficult to understand. Specific examples are always easier than more general principles.Anyway, using this general
scanfunction, the author can implementaccumSumin terms of it.accumSumsimply becomes a call to thescanfunction with some specific parameters. The newaccumSumis a lot more declarative than the previous one:And the amazing thing is that these functions and data types and whatnot are user defined and all, but it really seems like they're part of the language.
The thing that tells me a language is very high level is when you can extend the language with your own operators, functions, data types and whatnot, and in a blind test, a random programmer should think your stuff is part of the language.
Precisely.
I have thought about doing some CUDA, and I even started downloading the toolkits and all, but something got in the way. Yes, programming for CUDA would be a prime example of parallell programming.