77 points · by mqt · 17y ago · 94 comments
>> printIfNotNull(foo.getBar().getName()); // pointless method
>> in arc ...
>> ... aif is a macro that expands this code to
>>
>>(let it ((foo 'bar) 'name)
>> (if it
>> (print it)))
Why is a macro not pointless, while a method/function apparently is? I don't think that makes sense personally. Surely a macro is just a function that's been inlined - which may be better, depending on if you're optimizing for speed or size. Anyone explain?Also, I'm not sure if this was being serious or not:
>> Priorities in language design
>> A Language Should ...
If it was serious, it completely depends on what problem you're trying to solve as to what priorities you're going to have. ifNotNull(foo.getBar().getName(), function (it) { print it; });
but "function (it)" and all of the "().{}" are still blub. As PG points out on the arclanguage forums [1], an even simpler way of putting it is: (only.print foo!bar!name)
Which, without the Arc-specific syntax sugar, is: ((only print) ((foo 'bar) 'name))
So "only" is a modifier that makes a verb (function) happen only if its argument is truthy (think Python decorators).Note to PG: the dot operator is undocumented, and f.g.h.3 does not at all what I expect - looks like it does (f g h 3), whereas I expected (f (g (h 3))), in the spirit of Python decorators.
(def only (f)
(fn args (if (car args) (apply f args))))
The . and ! operators got redefined so that x.y.z is ((x y) z) instead of (x y z). I think I posted the source on arclanguage.org.ifNotNull(foo.getBar().getName(), printIt);
Granted, in java passing functions around is more of a pain.
The strength and danger of macros is in allowing you to create new syntax, skipping all the extra blub that made your code conform (consistently) to the original parser. What I'd like to know though is how much humans depend on that blub.
It's one thing to write your own macros and use them consistently, or even for a group of people to come up with macros and use them consistently. But if people work independently, what happens when you read someone else's code and find they've come up with macros to do the same things as yours do, but with different names and different usage conventions?
Wouldn't that be a bit like having every scientist invent their own jargon?
Either humans can learn to translate that kind of thing on the fly, or they can't, and lisp with heavy macro usage will only ever be useful as a solo (or small group) language.
Also, it's worthwhile to remember that aif does a bit more - it's semantically the equivalent of cond, which sometimes eliminates a ton of if/else blub.
It's not a linear continuum, with some "most powerful language" at the top. Different styles of languages have different strong points and weak points.
I, for one, find uniqueness types to be an easier to understand and use solution to the problem of maintaining referential transparency in the face of a stateful universe.
As novel as this may seem, most people who regularly program in languages other than Lisp also consider excessively verbose code to be a bad thing. (Hence the interest in refactoring.) If you're dealing with someone who just started programming a few months ago, they don't have enough experience to know why it's problematic, but then, they probably need someone to be patient and helpful, rather than smug.
You can say, "There are some things that are inherently hard to express in some languages that come naturally in others, and until you have experience with a couple different styles of languages, you'll only really know techniques used in your main language. Learning new languages adds new techniques to your problem solving toolkit. It takes a while, but it'll make you a much better programmer in the long run.", and it doesn't make you sound like you're sneering inside.
If you want to make the point that the way their language's object framework is designed makes any nontrivial program in it have a bunch of repetitive boilerplate, say that. Name-calling makes people defensive, not likely to consider new ideas.
x = (a = 7 * (b = 14)) + (c = 9 * ((d > 7)? e:f));
Now toss in some casts and the line becomes harder to read, but at the same time it's considered poor from to have 4 assignments on one line. Yet that's valid C, C++, Java, C# etc and it's got fewer symbols so it must be better according to the arc view of the world.
So is reqiring one assignment per line cruft or a good idea? How about those assignments? I would argue that one of the most useful mesures of a language is how easy it is to read other peoples code, but then you end up with something like Pascal...
PS: I acutaly like mantaining pascal code more than most languages then again it could have more to do with the experence of the people who use Pascal.
Besides, I don't think anybody is really expecting you to cram as much into one line as possible.
"Boilerplate" has that meaning, but without the connotation that the person using it is automatically a moron. (http://en.wikipedia.org/wiki/Boilerplate_%28text%29)
See also: Simon Peyton-Jones's "Scrap Your Boilerplate" papers (http://research.microsoft.com/en-us/um/people/simonpj/papers...)
Also in modern Java (and Ruby) projects you can typically write:
logger.debug("This is a stupidly expensive calculation: " + stupidlyExpensiveCalculation())
and it will get executed regardless and logged only if your logging settings, which are configured elsewhere and can be toggled with a mouseclick, are set to log the debug level.
Now, don't get me wrong, Arc's solution of defining a macro which checks to see whether your debug setting, which is defined elsewhere and can be toggled with a mouseclick, is on or not is a good idea. Its just not a revolutionary new idea for us wizened Big Freaking Enterprise App Ugly Language Programmers. Nor was it really that new in the halycon days of yore when Log4j crawled out of the primordial ooze.
Seems to me you say this like it's a good thing. Or I am misunderstanding...
isDebugEnabled() method is used to scare people since 2001, probably around the time when it came out of common use.
Macro that is being used for exactly that is not a distinguishing feature of the language. Java proponents may come out with, say, accessing array in constant time (how many car/cdr pairs you need for that?)
Important is that neither of the described facts add something to the value of one language against another and usually serve only as a basis for speculation.
Edit: when I'm saying "went out of common use", I mean that elegant alternatives like
logger.log("Expensive calculation is going to be: {0}", objectWithExpensiveToStringMethod);
have emerged.
logger.debug {
...
}
Which can be done trivially in a language with convenient blocks/lambdas. Then the debugging expressions will not run at all with debugging disabled.Yes, having a meta-syntax as your syntax is very useful.
In this case, that just slows things down. Worse yet is when it is stupidCalculationWithSideEffects(). And I have seen that before. :(
When I develop software, most of the big delays, and excuses for procrastination are in forcing my brain into action over the higher level concepts. I find a lot of the extra lines of code required when using BLUB can be written at high speed, with little "hurt" to the brain.
I'm not saying BLUB is just as fast to develop with, I'm just saying the benefit isn't as good as number of lines ratio good.
One of the more complicated features I waited to implement in the web version, just because I found it rather difficult to think of the problem in VB. The javascript code was just easier to write and think in, and once implemented I could translate it to VB.
Although I do not know if there is a limit to the benefits of moving to a smaller language. Also, this is just my personal experience and I do not know how much it generalizes to other people.
But I think during the writing of the program, you type the code, review the code, look at some other code, come back to that code, so you are writing/editing/modifying a particular piece of code multiple times.
I have a superstition that there is an element of polynomial effect on the size of the resulting program on the time to get it working or to parse code written by another person.
"One way to design a language is to just write down the program you'd
like to be able to write, regardless of whether there is a compiler that
can translate it or hardware that can run it."
Test driven really works, even for language development.Maybe it's indicative of my crowd and not the community at large, but even the smartest people seem to hit up Haskell-for-fun and Python-in-practice over Arc. It's hard to call them blub-programmers, since they're genuinely curious, hard-working people...
I am in exactly that camp, tho' I do fully intend to use Haskell for real once it becomes viable to do so. It's like the Python Paradox all over again.
In Io, the meanings of symbols are determined when the block is activated.
and
Though classical Lisps all have funcall and apply (not sure about Arc).
makeOriginalScope := block(name := "originalScope"; block(writeln(name)))
block(name := "newScope"; makeOriginalScope call setScope(thisContext) call) call
//newScopeIn my programming whenever I see myself writing blubber (I like that word), I just factor it out into a separate procedure and give it a descriptive name.
So when I'm reading the code of the calling routine, I rarely see blubber; it's contained in simple routines that are easy to read/verify.
Most of the examples here amount to thoughtful library development. With a good library, C++ and macro processor can also many of these things.
Here's a version of aif that works for most cases (I'm not claiming that C has the expressive power of Lisp):
#define aif(expr, val, code...) \
{ if ((expr) != (val)) { \
code ; \
} \
}
IMHO, this is good enough for blubber reduction. #define aif(expr, code) \
{ \
int it = (expr); \
if (it) \
{ \
code; \
} \
}
But this is still not close because in a Lisp, if is an expression that returns a value. We can't use the ternary operator because then there's no place to declare the "it" variable. You can't have an expression that declares a variable local to that expression in C no matter how hard you try.And the above lacks the "do {} while (0)" weirdness needed to make a cpp macro behave syntactically roughly like a function.
For example, it looks like Arc uses the "t" as a global true value. Is it really safe to assume "t" wouldn't be accidentally used as a local variable?
public void keyPressed(KeyEvent event) {
}
public void keyReleased(KeyEvent event) {
}
});
About Arc code: (on-key-press frame
'u (rotate-shape)
'j (drop-shape)
'h (move-shape -1)
'k (move-shape 1))
Is this code ADDS NEW key event handler OR OVERRIDES old one? When it is invoked - when key is PRESSED or when key is TYPED?There's the `printIfNotNull(foo.getBar().getName()); // pointless method`, also pointed out by axon, and the following code being duplicated instead of being a function:
if (log.isDebugEnabled()) {
log.debug("foo.valueOf(expression) = " +
foo.valueOf(expression));
}
BTW: heads up on your comment formatting: HN needs a blank line between text and code for 4-space indentation to be recognized.here's a way where events are objects... rather than an invocation. Much more flexible.
a python + pygame example...
cu,
key_handlers= dict(u=rotate, j=drop, h=lambda : move(-1), k=lambda : move(-1))
buttons = {1:'u', 2:'j', 3:'h', 4:'k'}
for e in pygame.event.get(): if e.type == KEYDOWN: key_handlers[e.key]()
elif e.type == JOYBUTTONDOWN:
key_handlers[buttons[e.button]]()
elif e.type in [QUIT, MOUSEBUTTONDOWN, HTTPD]:
quit()The arc one is also less powerful, because it uses different function invocations for different types of events.
It's also likely more people will understand the python+pygame version. More people do currently understand the python version... hardly anyone uses Arc.
You need to understand non-common concepts. You also need to understand how to create functions - rather than just call functions.
Does that Arc one mean when the key down happens, or when a key up happens? Or maybe it means in between the key press and key down. Also, when will that code be called?
It's magic. Will it be called from a separate thread, or some other Magic time... before or after the screen is to be updated? What else is going on at that time in the program? With the Arc version you have no idea... it's not explicit.
Also Arc doesn't have first class events. It could have yes, but it doesn't.
ps. the python code above was mangled by the buggy Arc program running this forum.
Here is the code redone for simplicity without the extra shortness and power expressed in the other version.
.
for e in pygame.event.get():
if e.type == KEYDOWN:
if e.unicode == 'u':
shape.rotate()
elif e.unicode == 'j':
shape.drop()
elif e.unicode == 'h':
shape.move(-1)
elif e.unicode == 'k':
shape.move(1)
Or the declarative python version:.
dict( u=rotate , j=drop , h=lambda:move(-1) , k=lambda:move(-1))