Kotlin logo

Kotlin

A concise multiplatform language developed by JetBrains

Language design

Kotlin gets support for S-expressions

Please note that this post is an April Fool’s Day joke.

Kotlin is always happy to learn from other programming languages, and this is why we decided to support S-expressions, the empowering concept of LISP.

The main motivation behind this is the compatibility with Clojure, the LISP for the JVM. Clojure is well-known for its solid libraries, particularly the ones used for concurrency and read-only data structures.

To facilitate interoperability with LISP (and Clojure libraries in particular), Kotlin now allows expressions like this one:

 (println (
        (lambda (arg1)
            (+
                "first lambda: "
                arg1
                (lambda (arg2)
                    (+ "second lambda: " arg1 arg2)
                )
            )
        ) foo "bar"
    ))

This is only to give you the taste. Now, let’s explain the constructs one-by-one.

S-expressions explained

First of all, there’s only one new syntactic construct added, namely the S-expression. It has the form

(A B C ...)

where A, B, C … may themselves be S-expressions or other Kotlin expressions.

Now, most operations in LISP are written in prefix form, and if you want, you can write addition in this style in Kotlin:

(+ 1 two 3)

Note that literals (‘1’, ‘3’) can be mixed with other expressions (e.g. ‘two’).

LISP originally stands for LISt Processing, so the literals for lists are very important:

(list abc "def" ghi "jkl")

This create a list of four objects.

Normal Kotlin functions can be called in the LISP-manner, so to print the list above, we can say:

  (println
       (list abc "def" ghi "jkl")
  )

Lambda expressions also have a LISP-form:

(lambda (arg1) (+ 1 arg1 2))

And the following code demonstrates closures:

    (println (
        (lambda (arg1)
            (+
                "first lambda: "
                arg1
                (lambda (arg2)
                    (+ "second lambda: " arg1 arg2)
                )
            )
        ) foo "bar"
    ))

Try it out!

You can find the above examples (and some more) here. They are runnable, and you can play with the code. Disclaimer: it’s only a prototype.

Limitations

Unfortunately, at this stage our support for S-expressions is somewhat limited. Due to some issues connected with parsing, an S-expression can only be of odd length. We are working on removing this limitation.

Also, when defining a named function as an S-expression, its name has to be preceded with a dot (not to be confused with the Dot operator mentioned below):

(defun .baz (a, b, c) (+ a b c)) 

Dot Operator

Many of you were waiting eagerly for us to release the Dot operator. Now it’s implemented, compiler and IDE plugin are downloadable here, thanks to jgl87.

image description