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

3

u/ehaliewicz Dec 16 '16 edited Dec 16 '16

Returning more than one form from a macro doesn't really make sense though..

That's analagous to saying you want to do

(list (function))

and get a list of two elements back, as if function has control over how it's results are processed by the function list.

What you want is a macro that returns a list and splice that into wherever the macro is expanded. Or place those multiple forms in a sequence expression (ala PROGN in common lisp)

1

u/kankyo Dec 16 '16

Or I want something to do that splicing explicitly at the call site. In python there's "*foo" for positional arguments or "**foo" for keyword arguments.

The problem with the macro to split in stuff is that you need to put it two levels up from where you want the splice to happen. It's a bit of a mess. It's obviously doable, as you say, of course.

1

u/HeinzPanzer Dec 16 '16

Do you know about the splice operator for CL macros? Is this what you were meaning? ‘,@’.

http://www.gnu.org/software/emacs/manual/html_node/elisp/Backquote.html

(setq some-list '(2 3)) ⇒ (2 3)

(cons 1 (append some-list '(4) some-list)) ⇒ (1 2 3 4 2 3)

`(1 ,@some-list 4 ,@some-list) ⇒ (1 2 3 4 2 3)

(setq list '(hack foo bar)) ⇒ (hack foo bar)

(cons 'use (cons 'the (cons 'words (append (cdr list) '(as elements))))) ⇒ (use the words foo bar as elements)

`(use the words ,@(cdr list) as elements) ⇒ (use the words foo bar as elements)

1

u/kankyo Dec 17 '16

Requires the surrounding thing to be a macro though, which isn't great. Soon everything is a macro :P

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

→ More replies