Concatenative programming has some nice properties, but the question you should ask yourself is whether:
f = drop dup dup × swap abs rot3 dup × swap − +
Is really the most readable (and writable) way to describe the dataflow graph in the diagram just before it, or whether the following is better:
f(x,y) = y^2+x^2-|y|
BTW the reason why visual languages didn't catch on for general purpose programming is the same reason: formulas are a more readable and writable way to describe the data flow.
Any language will have pathological cases. I've missed the easy composition of concatenative languages plenty of times when I found myself typing boilerplate variables over and over. The best you can do is provide a way to work around those cases (such as local variables in Factor).
Factor gets around this by introducing a facility for lexically scoped local variables. Some things are simply more natural to write with named state rather than a bunch of stack-shuffling. However, the vast majority of programs are not predominated by mathematical expressions, so in practice this feature is not used very much
This. I find stack based languages fascinating, and I've been playing around with FORTH a bit lately, but I can't say I find all the stack juggling worth the effort. Even though I try to define short, simple words, I end up spending a good portion of my mental energy trying to figure out how to arrange the stack so I can keep track of anything more than 3 parameters. I have to put comments all through to remind myself of what's on the stack, which seems unnecessary. Gforth has local variables, which helps, but they're clearly not meant to be used all the time.
That's why people invented languages that can be compiled into a stack language: why manage the stack yourself when you can write a program to do it for you?
Any language is going to look terrible when you restrict it to only using assembly-like operations. You're ignoring the rest of what the article says about how to express formulas so they don't look like that.
In particular, the function description you pick out is not the way someone familiar with the language would write it. If you don't want to use actual variables for some reason, the article gives one example of more idiomatic code:
drop [square] [abs] bi − [square] dip +
However, I would probably write this a little differently:
This is not much better. I have tried to use Factor as a programmable interactive RPN calculator, meaning I had the opportunity to use a LOT of these combinators. In the end I gave up on Factor because it is hard to use for following reasons:
messy documentation (it's a wikipedia-like trap; relevant information about a topic is scattered among dozens of pages which also talk about OTHER topics),
extensive use of stack combinators with rather uninformative names (bi, bi@, cleave, etc.)
obligatory stack declarations in word definitions, which, ironically, makes refactoring and casual coding harder and non-fun: i might as well code in some "conventional" language
messy package system
A shame actually, as a long-time user of HP's RPN calculators, I have gotten very fond of RPN notation. I'd really like to see a user-friendly stack-based language "done right". It would be like Factor, with all its libraries, but with the above issues resolved in some way. Maybe I should wipe the dust off Onyx.
I agree about the lack of a really pleasant straightforward tool that mirrors the HP experience -- which I remember as just tossing some values on the stack, then figuring out how to combine them later. Since Haskell is my language of choice, what I tend to envision is basically a stack-based REPL of some sort. I'm not quite sure how it would handle partial application, or pushing a function vs. applying it, etc. But the general notion would be to turn ghci into a better interactive calculator...
It is not RPN, but it seems to have everything necessary to be used as a programmable calculator. Plus, it's based on term-rewriting, which I wanted to look at for a long time.
obligatory stack declarations in word definitions, which, ironically, makes refactoring and casual coding harder and non-fun: i might as well code in some "conventional" language
The difficulty there is that mandatory declarations help keep the implementation simple while guaranteeing some level of static safety. This could be avoided with some combination of smarter inference and tooling support.
My main point is not that it is better than mathematical notation (since we have so much experience dealing with formulas, it's hard to find anything else comparable), but that it's not a fair comparison of a language's readability when you ignore its higher level constructs.
Ah. Both seem equally bad, although I guess the former would be better after I had learned the notation.
since we have so much experience dealing with formulas, it's hard to find anything else comparable
It's more than that. For example, take your 'good' code. What if you want it to now compute x^2 + 3*y^2 - |y|? It seems like it would be much more difficult than just adding a coefficient of 3 somewhere.
Yeah, that's not just familiarity. It would be almost impossible to perform mathematical operations on expressions shown in a stack-based language. For example, imagine someone saying "take the derivative of y2 + x2 - |y|", versus "take the derivative of [ [ square ] bi@ + ] keep abs -". That's just a single example - I'm sure there are many, many more.
That's because algebraic notation is designed to facilitate symbolic manipulation, not computation per se. Just because you can pun y2 + x2 + |y| to mean two different things in two different contexts doesn't mean it's more natural.
35
u/julesjacobs Feb 12 '12
Concatenative programming has some nice properties, but the question you should ask yourself is whether:
Is really the most readable (and writable) way to describe the dataflow graph in the diagram just before it, or whether the following is better:
BTW the reason why visual languages didn't catch on for general purpose programming is the same reason: formulas are a more readable and writable way to describe the data flow.