Arc Forumnew | comments | leaders | submitlogin
symbol macros, please?
5 points by kennytilton 6114 days ago | 11 comments
If Arc wants to get briefer I should think symbol macros would be a great addition. This is great example of variable capture as well:

Background: In my CL code base I have extended the object model to support slots that work like cells in a spreadsheet. I will be releasing a lite version of that for Arc tonight. I have found it useful editing-wise to consistently refer to the primary object (in Arc's case table) as "self", liking Smalltalk and loathing C++. But I digress. Now I happen also to use a base Family class to great good effect all over the map, so I am always coding up (parent self) and (kids self) and even (car (kids self)) and (grand-parent self). //EOB

With symbol macros I can:

  (define-symbol-macro .gramps
     (parent (parent self))
And then:

  (def count-cousins (self)
     (1- (loop for uncle in (kids .gramps)
            summing (length (kids uncle)))))
Which reminds me, maybe Brevity the Prime Directive will make Mr. Graham reconsider CL's LOOP (perhaps calling it ITERATE since LOOP is taken).


2 points by absz 6113 days ago | link

I'm not a CLer, so I've never used loop, but in reading this example, couldn't you write it as

  (def count-cousins (self)
    (- (reduce + (map [len (kids _)] (kids .gramps))) 1))
? Or even, if I remember reduce properly, as

  (def count-cousins (self)
    (- (reduce [+ _1 (len (kids _2))] (kids .gramps)) 1))
? (Though that requires the multi-argument [] form on the git "wiki"; you can of course do it as a normal fn, though.) I personally love map, reduce, keep, etc., and find them to be incredibly effective for working on lists.

However, I do quite like the idea of symbol macros. There's a nicety to .gramps that (.gramps) doesn't have. (Setting aside the fact that with the new . syntax, .gramps is an illegal name.)

-----

2 points by cooldude127 6113 days ago | link

or maybe mr. graham will consider cl's iterate package, which is more sane and lispy than loop.

-----

3 points by kennytilton 6113 days ago | link

The weird thing is that loop seems Arcy-er than iterate cuz it like Arc goes out of its way to minimize parentheses. Worth the learning curve, I assure you.

-----

2 points by vsingh 6113 days ago | link

Didn't Paul call Loop one of the worst mistakes in Common Lisp? I doubt the man is going to change his mind on this one.

Loop may minimize parentheses (i.e. nesting), but then again, so does BASIC. I'd say the main reason it isn't "Arcy" is that it fails the simplicity-of-implementation test.

-----

2 points by kennytilton 6113 days ago | link

I think you misunderstand. The "weird" to which I refer is precisely pg's anti-(cl)-loopism. He should love loop not just for the brevity, but also because it is a triumph of DSL. ie, CL loop is a DSL for iteration, and I know because I use it for everything. But! pg seems closer to being a schemer at heart so maybe he prefers recursion for things I handle by iteration.

ps. Are you saying BASIC eliminates parentheses the same way LOOP does? :)

-----

1 point by vsingh 6113 days ago | link

It's hard for me to see Loop as a triumph of DSL. I see it more as a failure of Common Lisp to provide simple flexible iteration constructs.

Arc already seems to provide simple operators for many of the cases in which Loop is commonly used. For example: 'accum', 'for', and 'repeat'. It doesn't seem like we need Loop.

Re BASIC: In a way, yes. Both BASIC and Loop use keywords to replace the indication of structure by parentheses. For example, we could eliminate a pair of brackets in 'with' by doing this:

    (with x 2 y 3 z 4 endvars
        (+ x y z))
but that wouldn't be very Lispy.

-----

2 points by kennytilton 6112 days ago | link

Well I resisted Loop for almost ten years then broke down and learned it when PCL came out cuz it had a good chapter on it, so having made the transition I can assure you it is a powerful little iteration language, not just a simple iterator of which CL has many so it is not clear what you do not like about those. I'll ignore your continued insistence that BASIC has anything to do with this discussion. :)

Btw, loop offers a little-known second syntax that is Lispy, one just never sees examples in the wild.

-----

2 points by vsingh 6112 days ago | link

If you have found through long experience that Loop truly offers something unique that can't be offered as well (or as intelligibly) by a combination of simpler operators, then I'll have to take your opinion seriously.

I've made a thread to collect exemplary examples of Loop in action (http://arclanguage.org/item?id=2938) and it'd be great if you could contribute your favorites, e.g. from Cells or other code you've written.

Personally, I don't have the same experience as you. I've found Loop to be useful as a replacement for iteration constructs that should have been in CL to begin with, e.g. dovector, when I'm too lazy to code the requisite macro. I haven't ever had to use Loop in its full complexity.

-----

4 points by kennytilton 6112 days ago | link

Sorry, it just sounds from what you have posted so far that you do not know CL's loop, by which I mean make an effort to use all of its capabilities such that you would know what's there so well that you had it at your fingertips. I am having the same problem with CL idiots denouncing Arc who probably have not even installed it.

You want me to code (dotimes (x 10) (foo))?! No way!

  (loop repeat 10 do (foo)). 
And that is not even a good example!!

  (let (x)
     (dolist (y whatever (nreverse x))
        (let ((py (pfft y)))
           (when py
               (push (yo-mama (cons y py)) x)))
Egad! How about:

   (loop for y in whatever
         for py = (pfft y)
         when py collect (yo-mama (cons y py)))
Is the first version starting to look like a disassembly? Bingo!!! :)

btw, the thing that got me to break down and give loop a chance was learning that the expansion was highly optimized code, meaning (for example) it would not first map across whatever and then delete the nils.

but these examples still do not get to the point of LOOP being a DSL. That property emerges only in the next level of application, as even the loop form expands to ten lines. But once you have loop under your belt (I kill myself) you do not even want to code a simple dotimes, that bogus unused count variable just pisses you off no end, never mind all the extra (wait for it) parentheses!

pg, phone home, all is forgiven! :)

-----

2 points by vsingh 6112 days ago | link

That looks interesting. I've submitted that example to my thread.

-----

1 point by almkglor 6113 days ago | link

/me loves 'loop

-----