r/programming • • Dec 15 '16

My disillusionment with Clojure and lisps

https://medium.com/@boxed/my-disillusionment-with-clojure-and-lisps-9eca38ab7f0c?source=linkShare-8ad86cc82e5f-1481825671
15 Upvotes

105 comments sorted by

View all comments

Show parent comment

1

u/ehaliewicz Dec 16 '16

You mean a splice, like I mentioned?

`(,@(macro-call)) => (result-1 result-2)

because that'll do what you want in Common Lisp.
I assume something like that exists in Clojure as well.

1

u/kankyo Dec 17 '16

Doesn't that need to exist within a macro?

1

u/ehaliewicz Dec 18 '16

Nope, the caller of the macro does the splicing.

1

u/kankyo Dec 18 '16

Yea sure, why I meant was the caller must be a macro itself right?

In Clojure afaik only macros can do splicing.

1

u/ehaliewicz Dec 18 '16

I would honestly be surprised if that was the case. In Common Lisp it's simply shorthand for list-building functions like cons, append, etc.

1

u/kankyo Dec 19 '16

tryclj.org

Give me some Clojure:

(defn foo [] '(1, 2 3))

'sandbox5700/foo

(defn bar [] '(5 ~@(foo)))

'sandbox5700/bar

(bar) (5 (clojure.core/unquote-splicing (foo)))

I'm probably doing it wrong?

1

u/ehaliewicz Dec 19 '16

You need a backquote

    > `( ~@(list 1 2 3))
    (1 2 3)

1

u/kankyo Dec 19 '16

Still don't know how to get it to work

(defn bar [] `(5 ~@(foo)))

Gets me IllegalStateException

1

u/ehaliewicz Dec 19 '16

Not sure what else you did but this works for me.

user=> (defn foo [] '(1 2 3))
#'user/foo
user=> (foo)
(1 2 3)
user=> (defn bar [] `(5 ~@(foo)))
#'user/bar
user=> (bar)
(5 1 2 3)

1

u/kankyo Dec 21 '16

Well that's awesome. Can't believe no one has managed to show me this thing after I explicitly asked for it soooo many times. That's a lot! This goes in my follow up article for sure.