Arc Forumnew | comments | leaders | submitlogin
4 points by kennytilton 6114 days ago | link | parent

CL keywords are a plist which must be parsed? Yeah, but (loosely speaking) by the compiler, not by me.* For example, in CL I can write your cut thus:

  (defun cut (seq &key (start 0) (end (length seq)))
     ;no parsing here
     (subseq seq start end))
...and then call it like this:

  (cut "Hello World" :start 2 :end 8)
(I made the seq parameter mandatory because I cannot understand why else I would be calling cut.)

keyword args make code more readable and as bogo said are extremely handy for those occasional functions with a kazillion parameters.

* One /can/ combine &rest all-args &key key1 key2 &allow-other-initargs and then parse the all-args as a plist if in a black-belt kinda mood, but it is exceedingly rare to find in normal code.



2 points by are 6113 days ago | link

Arc currently supports optional parameters.

A CL-style keyword argument is like an optional argument in that you need to give it a default value for when it's omitted. But in addition, with a keyword argument at function-call-time, you can switch argument order if you specify the key.

This means that Arc could treat keyword arguments as a special case of optional arguments.

This is the optional-arg example from the Arc tutorial:

arc> (def greet (name (o punc (case name who #\? #\!)))

       (string "hello " name punc)) 
* redefining greet

#<procedure: greet>

arc> (greet 'who)

"hello who?"

But let's say you allow both "o" and "k" for optional arguments, and when there is a "k", you allow the argument symbol to be used as a key when calling the function, like this:

arc> (def greet (name (k punc (case name who #\? #\!)))

       (string "hello " name punc)) 
* redefining greet

#<procedure: greet>

arc> (greet 'who)

"hello who?"

arc> (greet 'john)

"hello john!"

arc> (greet :punc "?!?" 'jane)

"hello jane?!?"

Of course, once you allow an argument symbol to be used as an argument key when calling, you could actually extend this opportunity to every single argument, optional or not. Then you would no longer need the "k" syntax, since every argument is "keyable":

arc> (def greet (name (o punc (case name who #\? #\!)))

       (string "hello " name punc))
* redefining greet

#<procedure: greet>

arc> (greet 'who)

"hello who?"

arc> (greet :punc "?!?" :name 'jane)

"hello jane?!?"

-----

1 point by are 6113 days ago | link

And BTW, the :arg syntax for argument keys is only a CL convention. In Arc, it would be much more natural/readable to use arg: (colon appended rather than prepended to the symbol).

-----

1 point by AF 6112 days ago | link

Is that even a possibility? Doesn't : currently do function composition in Arc?

-----

1 point by are 6112 days ago | link

Wouldn't there be whitespace after an argument keyword, distinguishing it from function composition (no whitespace)?

-----