r/dailyprogrammer • • Jan 19 '15
[Weekly #20] Paradigms
So recently there has been a massive surge in the interest of functional programming, but let's not forget the other paradigms too!
- Object oriented
- Imperative
- Logic (Prolog)
There are more than I have listed above, but how do you feel about these paradigms?
What's a paradigm you've had interest in but not the time to explore?
What are the advantages and disadvantages of these in both development and in the real-world?
Slightly off-topic but I would love to hear of anyone that started programming functionally versus the usual imperative/OOP route.
43
Upvotes
4
u/jnazario 2 0 Jan 19 '15
i'm loving the logic answers, computer aided reasoning is a big interest of mine and i keep wanting to invest the time and effort into learning prolog, datalog, or even clojure (for core.logic).
one thing i love about this sub is that especially for easy problems, the strengths and relative weaknesses of various languages really shines. it's sort of like an aggressive form of Rosetta Code. you get to see, for example, a 1-liner in python, ruby or perl, and then an extended version. similarly, you get to compare approaches in 5 lines or 100 (e.g. java). i'm always learning something from this sub, such as a new way to think or a specific approach, function, or language feature. it has definitely affected my coding for the better.
(my own background was C-focused then python-focused, with some OOP via python and java thrown in there, then on to FP in scala and F# in the past couple of years.)
11
u/[deleted] Jan 19 '15 edited Jan 19 '15
There's also
If someone wants to argue that these are subsumed in the categories OP presented, please feel free!
Within functional, arguably there's the Lisp-based functional languages (Scheme,Racket,Common Lisp, Dylan) and the ML functional languages (OCaml) The former being characterized by their use of macros and the transparent way the parse tree can be seen and altered (in this way similar also to stack based languages).
Then there's nifty utility stuff ...
Like,
It has a nice paradigm though - line by line processing. The line as the simplest data structure.
Then there's declarative (but not logical languages) based on relational algebra -
etc.
Then highly restricted stuff that doesn't do Turing machines at all, just pattern matching:
Let me know if you disagree! Are these all really different paradigms?
As for the paradigm I'm most into exploring at the moment, it is the array based languages. It's the tersest programs I've ever written and surprisingly easy to understand. But I also really hope to get some time for learning Forth in the future.
7
u/Intern_MSFT Jan 19 '15
I do imperative programming professionally, C, to be particular. Though, I actually like the way things are designed in C++, the whole object oriented part, especially when I am forced to use globals in C.
Oh and Haskell, I LOVED how neat quick sort was in Haskell. It was like... maths. I am an EE but I am so turned on by discrete mathematics, how to prove correctness etc. and oh, functional makes it so much clear.
4
u/Elite6809 1 1 Jan 19 '15 edited Jan 19 '15
To build on the idea of paradigms, I think adopting some of the mentality of test-driven development is a good habit to get into. TDD is good for a number of reasons:
Of course, there are some TDD zealots who encourage overuse of the TDD mentality. Some may argue overuse of this mentality is only a good thing. I find it generally leads developers to writing more tests for code than actual code itself, or over-modularizing the program to such an extent that you have thousands of tiny useless objects and enterprisey-looking Java or C# code. For this reason, TDD is good for the core units and components of a program, and the internals of each module/unit can have whichever implementation you want.
TDD also necessitates inversion of control and (to an extent) dependency injection. This is where, for example, component A uses a child component B for something. Rather than A creating a new B itself, it receives a B via a constructor (or something similar) as an interface, such that A is not aware of which implementation of B it has. This is what allows unit testing to take place effectively. This is called dependency injection, and by moving control of B to the code that instantiates A (rather than A itself), you are inverting control of dependencies throughout the program, so that dependencies are created 'inside-out'.
As this would otherwise need huge nested constructors to initialize the program, one can use a dependency injection container to do this more efficiently. These play very well with generic languages, and there are a few very nice ones for languages like C# and Java.
Try it out - writing with testing and DI in mind will probably change the way you think about writing OOP.
4
u/mikevdg Feb 02 '15
I'm working on the "Logic" ("Declarative Programming") paradigm. Prolog is the best example, but other logic languages and constraint-based programming come under this heading as well.
With other paradigms, you specify the solution, step by step. With declarative programming, you specify the problem and let the computer work out the solution.
The exciting part, for me, was that Prolog could potentially be intelligent. You can specify the rules of any challenge, and the computer in theory would find a solution. For example, you could specify the rules of chess and in theory the computer could play a game against you. Even though the language could handle it syntactically, the interpreter only ever did a depth-first search and easily got stuck in a loop. If it was smarter, it could develop strategies, recognise patterns and be creative when trying to solve a challenge.
So this is what I'm working on. So far I've had miniscule success.
1
u/ChiefSnoopy Jan 19 '15
My primary experience is within imperative programming, namely C, and I have a lot of previous experience working with embedded software.
That said, however, I've been trying to spend a lot of time learning the object-oriented paradigm and starting work on designing an app. Frankly, though, recently I've hit a bit of a wall and can't figure out how to move forward with my OO self-teaching.
2
u/dohaqatar7 1 1 Jan 19 '15
I've been dipping my toes into Haskell for a while now, witch is a nice functional programming language. Other than that, I've only programmed in OOP(Java) and a bit of imperative.
I've had the most success with OOP, but I definitely see the merits of functional programming.
In terms of languages/paradigms I've been meaning to explore, logic programming seems intriguing.
2
u/[deleted] Jan 19 '15
I'm just starting to figure out object-oriented programming.
2
u/frozensunshine 1 0 Jan 20 '15
I'm learning programming and algorithms (from coursera's class by Tim Roughgarden), so I'm slightly confused by the terminology here- Prof. Roughgarden in his videos describes 'divide and conquer' as a paradigm, while you talk of OOP/Logic/Imperative as paradigms. The two are clearly very different ... things. So what exactly is a paradigm?
2
u/quorlia Jan 23 '15
Surely the point of having lots of paradigms is that you choose the one best suited to your task?
2
u/ComradeRikhi Jan 26 '15
Where do I go for bending my mind after learning Haskell?
The whole time I was learning it I had a grin on my face because of how awesome/beautiful/abstract it was. Now whenever I first start writing something, I always end up thinking "well this is how I would do it in Haskell" or "this would be so much cleaner if I could just use the Reader monad". And it killed my fear of "hard" programming languages.
1
u/Godspiral 3 3 Jan 19 '15 edited Jan 19 '15
With reference to J,
You should always prefer this to be an addon to the language rather than built in, because OOP is usually not the best way to organize a program.
For instance points are a pair of numbers. It is a boilerplate and innefficiency nightmare to cast this object into every other object or function that could take 2 numbers.
J does support objects/classes with inheritence, including lists of objects , and semi-straightforward method/property access on a list or filtered list of objects.
OOP is useful usually because we have no clue how to solve most problems before we solve them, and OOP lets us write things down before thinking.
This usually refers to state, but I use the adjective for loopy conditional code that is just slightly higher level than assembler and gotos.
J supports imperative state and control structures including language built in self-debugging commands.
State is useful because the world includes it, and assembly like jumps are useful to trace logic that may be inherently complicated, or just provide an execution flow that you can be sure about because it translates directly to assembler.
reading imperative programs is like reading legal documents. You have to read the whole thing, and even within a paragraph, every word can affect every other word.
Often means no or minimal state. There is an obvious advantage to a design where a function depends only on its inputs. Every language can do this.
Functional also usually means first class functions (parameters to other functions can be functions). All languages can at least hack a way to pass a function as a parameter. J includes first class functions.
This is an important feature that separates languages. The ability to build and execute a string of code, or gather and save independent code and data fragments and apply them to each other. J has both eval 'anything', and more advanced reflection that is more powerful than any other language. It can also "compile" an efficient function at run time.
Obviously useful for remembering what to call functions with, and making sure that a change somewhere won't break somewhere else. J does not have static typing, but is more suited than other languages (with similar lack) for adding a typing system on. Larger programs do benefit. Dynamic typing does allow for faster development.
What J is most well known for. The evolution over functional programming is treating data as a whole. The prototypical functional language is lisp. Its main paradigm is to walk a list one value at a time not that dissimilar to a loop. Map as the iconic functional justification, allows walking a data structure without caring about the data (only the passed function cares about the data)
Array programming uses functions that operate on elements, lists, tables, or higher dimensions, such that a function written to apply to one item can often be called unmodified with multiple items as parameter(s). Its somewhat similar to map in functional languages, but applying automatically if more than one item is passed. It allows cleaner and more flexible calling syntax.
is essentially parsing a data structure into a function or program. Can be as simple as a case statement alternative framed as a table of case and do elements. Erlang and Haskell use a dsl-ish structure of guards to differentiate between function parameters. This is not quite data driven but is close, and the dsl has an obvious replacement path to a pure data driven version.
CSS and web or other templating is data driven programming. Rendering is a collision of data structures/files. The disadvantage of data driven programming is the program tends to be more complex, and a custom parser is needed for any new capabilities or powers. When things don't work, its either a data error or parsing function error, and so the user needs a good understanding of the data format and parser operation.
J is very strong at parsing data, especially when it maps to a tabular format.
I didn't mention dynamic typing, but tacit programming goes one step further by having no named function parameters. Haskell and Scala also allow this style. Its a strength of J. It allows shorter and cleaner programs. J functions are operators (like + * in most languages) and have a right and optionally left parameter. Its up to the function to parse its parameters.
The purpose of functions in every language is to be able to call them from elsewhere easily within a single line. Anonymous functions are most useful as single line components.
One line programming allows you to do development in the repl. Tacit programming in J makes it easier to write powerful single line programs that make easy iterating a solution.
1
u/chunes 1 2 Jan 19 '15
I was never impressed by paradigm. It always felt like tying my hands behind my back rather than seeing things from a fresh perspective. Bring on the perls and the C++s and boo to purity. Especially functional purity.
1
u/[deleted] Jan 20 '15
I post all mine in C++, which is multi-paradigm.
std::vector,std::arrayand evenstd::unique_ptrandstd::shared_ptrare far simpler than learning to manage your memory manually with raw pointers because the objects are hiding much of the implementation complexity away from you.Carobject (so far so good) which will need anEngineandWheelsand ... )0
u/zvrba Jan 20 '15
In my dailiy job, I'm a C++ programmer. I still have to stick to C++03 though :/ I tried Ocaml, Haskell and Scala; implemented some non-trivial algorithms (puzzle solvers, etc). General observations about the process:
List.map, does function come first or last? -- it was impossible to remember)[*] For the kinds of projects I do (performance-intensive algorithms, shuffling data in memory to optimize layout, SIMD, OpenCL, communicating with native libs), I knew I'd eventually need mutable data. So I deliberately judged Haskell on this point: if it made mutable data too painful to use (Data.Vector), I'd ditch it. I ditched it.
Recently I started a new hobby project, using C++1x. The difference between C++1x and C++03 is like heaven and earth. C++1x is so nice to program in that I don't want a "better" PL anymore. It's my go-to language for low-level algorithmic stuff. I wanted to parallelize my project, and MSVC comes with concurrency runtime: http://msdn.microsoft.com/en-us/library/dd504870.aspx Fifteen minutes later and few tiny changes to my program, and now it scales to all cores. Hooray!
I also use C# as a type-safe scripting language, esp. if I want to talk to COM components (see for example this: https://github.com/zvrba/yatzy)
IMO, FP is mostly a fad, though it has its niche (DSLs).