Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
gilch
@gilch
Macros get all of their arguments quoted. Because they run at compile time, their arguments haven't been evaluated yet.
So passing '(1 2) means it's quoted twice.
Kyle I S Harrington
@kephale
hmm ok
ah yeah that cadr fix does it
ok i'll do some comparisons with a python implementation as well
gilch
@gilch
So you have (quote '(1 2)) and only want the '(1 2).
If you had called it with (apply + (1 2)), then you wouldn't have to strip the quote.
Kyle I S Harrington
@kephale
yeah, i see, but i would get confused without the '(1 2)
gilch
@gilch
Yeah, apply is supposed to be a function, so it can get evaluated arguments.
Kyle I S Harrington
@kephale
:thumbsup:
thank you!
gilch
@gilch
You can certainly expand a macro to a function call though.
(defmacro apply* (f xs)
  `(apply ,f ,xs))
Assuming that Python definition.
Which you could write in Hissp as
(define apply
  (lambda (callable : args ()  kwargs {})
    (callable : :* args  :** kwargs)))
Or inline it like
Kyle I S Harrington
@kephale
i think that define is exactly what i was after!
gilch
@gilch
(defmacro apply (f xs)
  `((lambda ($#f $#xs)
      ($#f : :* $#xs))
    ,f ,xs))
There's really no difference between s-expressions and tuples in Lissp like there is in Hy. A quoted s-expression compiles to a tuple, and s-expressions read as Hissp tuples.
Whereas Hy has a separate HyExpression type.
gilch
@gilch
If I recall, Clojure's apply was a bit different.
Kyle I S Harrington
@kephale
thats what I thought (yeah the HyExpressions really threw me off). I kept ending up with results that were tuples, but I think I was missing a deeper concept
but Clojure's apply can work the same way at least
gilch
@gilch
def apply(f, *args):
    *args, star = args
    return f(*args, *star)
That one would work more like Clojure.
(define apply
  (lambda (f : :* args)
    (f : :* (getitem args (slice -1))
       :* (getitem args -1))))
Should be the equivalent of that Python.
gilch
@gilch
Maybe "quoted twice" is the wrong way to say it. The macro gets the Hissp code: ('quote', (1, 2)) and you wanted the (1, 2).
There was never a second 'quote' string in that process.
When I said that macros get their arguments quoted, I meant that they get the unevaluated Hissp, not that it's wrapped in quote forms.
I didn't really get any work done on Hissp today, but I can probably still get that PyPI release done in a week. Probably.
gilch
@gilch
I think I had also worded it as macros getting quoted arguments in the docs. I should reword that.
gilch
@gilch
You pretty much don't need apply given the star unpacking. Maybe if you needed it for a higher order function. But then something like %#(%1 : :* %2) should work.
Or %#(+ : :* %) if the function is fixed.
Kyle I S Harrington
@kephale
ok, I'm still understanding the ways of benefiting from Hissp's lambda implementation. that said i think i prefer using apply over directly using the reader macro with star unpacking, just for readability
gilch
@gilch
Given itertools.starmap and star unpacking, I don't recall ever missing apply in Python.
gilch
@gilch
Another advantage of star unpacking is that you can do it more than once in the same call, like (foo bar : :* spam :? baz :* eggs).
Kyle I S Harrington
@kephale
how do you make a list of strings? ["a","b"] doesn't work, although [1,2] does
gilch
@gilch
['a','b']
Collection atoms are injections, so they use Python syntax.
There are a lot of examples in the quick start: https://hissp.readthedocs.io/en/latest/lissp_quickstart.html
See the ;;;; COLLECTIONS section.
[\"a\",\"b\"] also works, but single quotes are easier.
gilch
@gilch
(list '(a b)) would also work, but beware of munging when using symbols as strings this way.
You might expect (list '("a" "b")) to also work, but that has to compile the quotes in to distinguish them from identifiers, resulting in ["('a')", "('b')"].
The right way to do that is (list `(,"a" ,"b")).
gilch
@gilch
Unlike the collection atoms, you can interpolate runtime values this way.
If you're doing anything complicated, like nesting these, or using them in macro templates, the overhead of managing the unquotes and the extra set of parentheses can make this difficult.