5

In addition to create list using parentheses, Clojure allows to create vectors using [ ], maps using { } and sets using #{ }.

Lisp is always said to be a very extensible language in which you can easily create DSLs etc. But is Lisp so extensible that you can take any Lisp dialect and relatively easily add support for Clojure's vectors, maps and sets (which are all functions in Clojure)?

I'm not necessarily asking about cons or similar actually working on these functions: what I'd like to know is if the other could be modified so that the source code would look like Clojure's source code (that is: using matching [ ], { } and #{ } in addition to ( )).

Note that if it cannot be done this is not a criticism of Lisp: what I'd like to know is, technically, what should be done or what cannot be done if one were to add such a thing.

8
  • Anything you can want can be added to other Lisps...its a matter of whether it becomes a standard. Lisp is infinitely expressive. Only a problem if your extensions rely on Java libraries.
    – Rig
    Jul 3, 2012 at 12:36
  • @Rig: not relying on any Java libraries (Clojure also work on the CLR and there's ClojureScript etc.) : ) Jul 3, 2012 at 12:46
  • Well if your changes rely on X library :)
    – Rig
    Jul 3, 2012 at 12:47
  • @Rig: but, technically, what would it imply? Is it something you'd do with reader macros? I'm really just talking about adding the support for vectors, sets and maps: not about making Clojure a standard or anything (there's much more to Clojure than just the [ ], { } and #{ } support btw, like lockless concurrency etc. but I'm not talking about that). Jul 3, 2012 at 12:49
  • Never done it but I know Lisp, in its power, has a means for it in Lisp's pure form. Someone here may have better knowledge on the "How" (or I would have answered) on what macro work might be needed
    – Rig
    Jul 3, 2012 at 12:51

2 Answers 2

7

Yes, they can be added. It is something you can do using reader macros. There is already #(…) for vectors, but you can define your own [...] macro:

(set-macro-character #\] (get-macro-character #\)))
(set-macro-character #\[ (lambda (stream char)
                   (declare (ignore char))
                   `',(apply #'vector (read-delimited-list #\] stream t))))

CL-USER> [1 2 3]
#(1 2 3)
CL-USER> (aref [1 2 3] 0)
1
CL-USER> 

Look at closer-apl for more&cooler examples.

1
  • this is totally amazing : ) Thanks for the link, beautiful... Jul 3, 2012 at 21:32
2

Trivial answer: yes, you can do this for any Lisp dialect by extending the implementation to provide more features. (I don't think this is what you mean).

Practical answer: no, you can't do this in "any Lisp dialect", although you can do it in some. All Lisp implementations are different -- they have different strengths, weaknesses, and ways in which they can be extended. Some may be opinionated, and not allow such syntax extensions when normal vector, map, and set functions along with parentheses could be used instead. Others may not allow it because it's another feature, which needs to be designed, specced, implemented and tested -- all of which increases the workload on the implementors.

If you have a specific Lisp in mind, you should check whether that dialect allows such a thing, but I wouldn't expect any Lisp code more complicated than (+ 1 2) to be portable to all implementations.

2
  • +1 to both answers... I had no specific dialect in mind: it's because I'd like to get the "bigger picture". Now funnily enough after reading your answer(s) I google'd a bit and realized that Clojure itself was the one being "opinionated" by not really allowing to mess with reader macro (at least not easily). : ) Jul 3, 2012 at 21:29
  • "but I wouldn't expect any Lisp code more complicated than (+ 1 2) to be portable to all implementations": I installed four different open source implementations of Common Lisp and all of them ran Slime, Quicklisp, and other tools without a glitch.
    – Giorgio
    Jan 8, 2016 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.