Arc Forumnew | comments | leaders | submitlogin
Merge let and with into one?
1 point by fkhdp 5787 days ago | 5 comments
While reading the tutorial i learned that let is for binding one name and with is for binding more than one name.

After implementing some macros for a particular Scheme project, i have noticed that i can merge the two into one macro: if the first argument is a pair, it behaves like with, if not, it behaves like let. This seems clearer to me since i can forget about one macro name, so i did it for my own code. I think this can be done in Arc too.

I wonder if there is some (more or less) subtle aspect of Arc making this impossible, so i ask you about that. If this is obviously a bad idea: please apologize -- i don't know.



3 points by absz 5786 days ago | link

This has been discussed before; as CatDancer said, the problem is auto-destructuring of lists. On Anarki, there are two macros, given and givens; they don't require the variables to be parenthesized, but can only have one expression in the body (which can, of course, be a do). given is analogous to with, and givens is analogous to withs. For more info, see http://arclanguage.org/item?id=6759 .

-----

5 points by CatDancer 5786 days ago | link

Arc allows you to use pattern matching with let and function definitions (and very convenient it is too!):

  (let (a b) '(1 2)
    (prn a)
    (prn b))
  1
  2

-----

2 points by CatDancer 5784 days ago | link

Oops, I think I misspoke when I described this Arc feature as "pattern matching"; iirc what "pattern matching" means is something that lets you compare a value against a list of patterns and select the first that matched. (Perhaps including setting pattern variables to the corresponding parts of the value, which would be like what this Arc feature does).

Hmm, "auto-destructuring" or even "destructuring" as what to call it, even if is standard usage, is quite the mouthful though...

-----

2 points by Darmani 5787 days ago | link

  (mac let/with body
    (if (isa (car body) 'cons)
        `(with ,@body)
        `(let ,@body)))
I'm not that keen on having the same simple macro use different syntax, though.

-----

1 point by fkhdp 5784 days ago | link

Thank you. I had a feeling that I am not the first one questioning this. The pattern matching in those cases looks convenient to me too.

I'm going to implement this for my own language...

-----