views:

425

answers:

5

I've read the wikipedia article on concatenative languages and I am now more confused than I was when I started. :-)

Can someone explain what a concatenative language is in stupid people terms?

A: 

You can't explain a language, just get one (factor, preferably) and try some tutorials on it.

http://factorcode.org/

http://docs.factorcode.org/content/article-cookbook.html

Tutorials are better than SO answerers.

alamar
That helps me understand *a* concatenative language. But it doesn't necessarily help me understand concatentative language *in general*.
Jason Baker
You shouldn't try to understand it *in general* until you learn one of these.
alamar
While his response doesn't answer your question, I'm gonna have to agree with alamar here. I would definitely pickup a Forth interpreter and a Forth book and try fiddling around. It's a different way of thinking about coding than most procedural languages do in terms of variables.
sheepsimulator
+3  A: 

From a link on the page alamar referred to: Concatenative Languages

I haven't had time to read through it yet, so I don't know any more than the address right now.

IanGilham
That's helpful and I think I've got an idea of what a Concatenative Language is, but it would be helpful to have a concise (maybe even oversimplified) definition of a concatenative language just to help me wrap my head around the concept.
Jason Baker
+9  A: 

In normal programming languages, you have variables which can be defined freely and you call methods using these variables as arguments. These are simple to understand but somewhat limited. Often, it is hard to reuse an existing method because you simply can't map the existing variables into the parameters the method needs or the method A calls another method B and A would be perfect for you if you could only replace the call to B with a call to C.

Concatenative language use a fixed data structure to save values (usually a stack or a list). There are no variables. This means that many methods and functions have the same "API": They work on something which someone else left on the stack. Plus code itself is thought to be "data", i.e. it is common to write code which can modify itself or which accepts other code as a "parameter" (i.e. as an element on the stack).

These attributes make this languages perfect for chaining existing code to create something new. Reuse is built in. You can write a function which accepts a list and a piece of code and calls the code for each item in the list. This will now work on any kind of data as long it's behaves like a list: results from a database, a row of pixels from an image, characters in a string, etc.

The biggest problem is that you have no hint what's going on. There are only a couple of data types (list, string, number), so everything gets mapped to that. When you get a piece of data, you usually don't care what it is or where it comes from. But that makes it hard to follow data through the code to see what is happening to it.

I believe it takes a certain set of mind to use the languages successfully. They are not for everyone.

[EDIT] Forth has some penetration but not that much. You can find PostScript in any modern laser printer. So they are niche languages.

From a functional level, they are at par with LISP, C-like languages and SQL: All of them are Turing Complete, so you can compute anything. It's just a matter of how much code you have to write. Some things are more simple in LISP, some are more simple in C, some are more simple in query languages. The question which is "better" is futile unless you have a context.

Aaron Digulla
Good explanation, but I'm left wondering why concatenative languages should ever be used in place of something like LISP - it just seems like a subset of the functionality that list-based languages can do. (In other words, could you elaborate on how a concatenative language's list/stack processing is anything but a more restricted version of other languages?)
Daniel Lew
Nice answer, with one nitpick - do you mean Turing Complete?
Dan Vinton
Another nitpick: SQL by itself isn't Turing complete. Although most DBMSes have extensions that make them Turing complete (this is what T-SQL, pl/SQL, etc mean)
Jason Baker
Fixed Turing :) And Jason's right: Without a loop that runs indefinitely, a language can't be Turing complete.
Aaron Digulla
This description brings JavaScript to mind, with how the lines between variables and functions seem to blur... is that at all right? I've never heard this term before either.
Alex Gosselin
There is some overlap in how different languages solve the same problem, so you'll see the same (or similar) solution in many places. What I'm still missing is a language which allows you to change the parser at runtime. I don't mean like LISP, I mean like "change the parser to include a piece of LISP in Java code without any escaping". Or maybe `HTML html = <html>...</html>;` with syntax checking and variable expansion right in a Java method.
Aaron Digulla
+4  A: 

To your simple question, here's a subjective and argumentative answer.

I looked at the article and several related web pages. The web pages say themselves that there isn't a real theory, so it's no wonder that people are having a hard time coming up with a precise and understandable definition. I would say that at present, it is not useful to classify languages as "concatenative" or "not concatenative". To me it looks like a term that gives Manfred von Thun a place to hang his hat but may not be useful for other programmers.

While PostScript and Forth are worth studying, I don't see anything terribly new or interesting in von Thun's Joy programming language. Indeed, if you read Chris Okasaki's paper on Techniques for Embedding Postfix Languages in Haskell you can try out all this stuff in a setting that, relative to Joy, is totally mainstream.

So my answer is there's no simple explanation because there's no mature theory underlying the idea of a concatenative language. (As Einstein and Feynman said, if you can't explain your idea to a college freshman, you don't really understand it.) I'll go further and say although studying some of these languages, like FORTH and PostScript, is an excellent use of time, trying to figure out exactly what people mean when they say "concatenative" is probably a waste of your time.

Norman Ramsey
Great answer. I'd never really thought that I might just not get it because there's not really a good explanation. :-)
Jason Baker
-1, while I agree that the terminology and the way that everyone is speaking about it is not very good, I boldly disagree that pursuing the idea is a waste of time. Sometimes it pays to learn languages that are different in structure and data manipulation technique to help our minds grow and discover new programming methods that may become helpful in certain situations. The fact that some people have a hard time talking about them doesn't make them a waste of time.
sheepsimulator
I didn't say the languages were a waste of time. PostScript and FORTH are great things to study. I said it was a waste of time to try to figure out exactly what people mean when they say "concatenative".
Norman Ramsey
@Norman Ramsey - good point, didn't read it clearly enough. -1 withdrawn.
sheepsimulator
+3  A: 

After reading http://concatenative.org/wiki/view/Concatenative%20language and drawing on what little I remember of fiddling around with Forth as a teenager, I believe that the key thing about concatenative programming has to do with:

  • viewing data in terms of values on a specific data stack
  • and functions manipulating stuff in terms of popping/pushing values on the same the data stack

Check out these quotes from the above webpage:

There are two terms that get thrown around, stack language and concatenative language. Both define similar but not equal classes of languages. For the most part though, they are identical.

Most languages in widespread use today are applicative languages: the central construct in the language is some form of function call, where a function is applied to a set of parameters, where each parameter is itself the result of a function call, the name of a variable, or a constant. In stack languages, a function call is made by simply writing the name of the function; the parameters are implicit, and they have to already be on the stack when the call is made. The result of the function call (if any) is then left on the stack after the function returns, for the next function to consume, and so on. Because functions are invoked simply by mentioning their name without any additional syntax, Forth and Factor refer to functions as "words", because in the syntax they really are just words.

This is in contrast to applicative languages that apply their functions directly to specific variables.

Example: adding two numbers.

Applicative language:

int foo(int a, int b)
{
    return a + b;
}

var c = 4;
var d = 3;
var g = foo(c,d);

Concatenative language (I made it up, supposed to be similar to Forth... ;) )

push 4
push 3
+
pop

While I don't think concatenative language = stack language, as the authors point out above, it seems similar.

sheepsimulator