The most exciting thing in the current Java world is to allow other programming languages to run on Java virtual machines. Discussions around JRuby, Groovy, Scala, and Rhino (JavaScript engines) have been rife. Why should it be stuck in a rut? If you really want to jump out of the mainstream and dive into a language that is very different from Java, Lisp is a good choice. There are several open source implementations of the Lisp programming language that can be run on the JVM, ready to embark on our journey of discovery!
What does Lisp deserve to be studied? First, as a language that has been in its 50-year history, it has contributed to many of the ideas that we take for granted today. The IF-THEN-ELSE structure, early object-oriented and automated memory management with garbage collection all come from this. The current hot topic of Java programmers--lexical closures (lexical Closure)--was first explored in Lisp in the 70 's. In addition, Lisp has features that many other languages have not yet adopted, and these excellent ideas will surely lead to revival in the future.
The target audience for this article is Java developers who are interested in learning about Lisp. We'll discuss in the next section the different Lisp dialects (dialect) that are currently available on the JVM to give you a quick idea of how the Lisp program works and its uniqueness, and at the end of the article will demonstrate how to integrate the Lisp code with the Java system.
There are a number of Lisp systems available for different platforms, free and commercial. For Java users who want to start exploring Lisp, it's easy to get started without leaving the JVM, and it's easy to use all of your familiar Java libraries and related tools.
Common Lisp and Scheme
There are two main dialects of Lisp (dialect): Common lisp and Scheme. Although the design concepts are broadly similar, their differences are still enough to cause a strong debate about the merits.
Common Lisp is the ANSI standard that was completed in 1991. Unified several early Lisp concept, is can be used in a variety of application development of large-scale environment, its most famous application is artificial intelligence. Scheme, which originated in academia, was deliberately streamlined and validated as a good language for both computer science teaching and embedded scripting language. You may also encounter some of the more famous Lisp: Small, application-specific DSLs such as Emacs Lisp or AutoCAD AutoLisp.
The two main dialects mentioned above (dialect) are implemented on the JVM in a way that is more mature than the schemes implementations. Armed Bear Common Lisp (www.armedbear.org/abcl.html) implements the Common Lisp standard very thoroughly, but there is a problem that if you do not install another Common list system, you cannot build the distribution version, This may be a problem for beginners.
In scheme terms, the two main products are Kawa (Www.gnu.org/software/kawa) and Sisc (www.sisc-scheme.org--the Second interpreter of scheme Code). In the example of this article, we'll use Kawa, which is actually a framework to create a new language that can be compiled into Java bytecode. Scheme is just one of its implementations. Incidentally, the creator of Kawa per Bothner is currently working at Sun, mainly engaged in the compiler aspects of JAVAFX projects.
Another worthy competitor is Clojure (clojure.sourceforge.net). This is a new language with a Lisp dialect (dialect) between scheme and Common Lisp. It is directly tailored to the JVM, so there is the clearest Java consolidation scenario in all of the lisp mentioned above. It also has some other exciting features, such as built-in support for concurrency and transaction memory. Clojure is still in the exploratory testing phase, so it's a bit premature to build a program on it, but it's definitely a noteworthy project.
Read-Evaluate-print-loop
Let's install the Kawa first. Its distribution is a separate jar file that can be downloaded directly from the link ftp://ftp.gnu.org/pub/gnu/kawa/kawa-1.9.1.jar. After you get the jar package, add it to your classpath so you can start the REPL by running the following command:
java kawa.repl
#|kawa:1|#
This command starts the Kawa and displays a prompt. What is the secret of this? REPL (read-eval-print-loop) means read-evaluate-print-loop, which interacts with the running Lisp system-it "reads" Your input, evaluates it, "prints" the results, and repeats "loops". The way to develop Lisp programs is different from the "write code, compile, and run" cycles we follow when developing Java programs. Lisp programmers need to motivate their Lisp system to keep it running so that the boundaries between compilation and runtime are blurred. In Repl, functions and variables can be modified during execution, and the code is dynamically interpreted and compiled.
Let's do something simple: Add two numbers together.
#|kawa:1|# (+ 1 2)
3
This is the typical structure of a Lisp expression, or "format." The syntax is consistent: the expression is always placed inside a pair of parentheses, because the prefix symbol is used, so the "+" number is placed before two parameters. And then a complex point structure that embeds several formats together to create a tree structure:
#|kawa:2|# (* (+ 1 2) (- 3 4))
-3
Scheme's built-in functions work in the same mechanism:
#|kawa:3|# (if (> (string-length "Hello world") 5)
(display "Longer than 5 characters"))
Longer than 5 characters
In the above program, an if statement is used to check whether a particular string is longer than 5 characters, and if the test result is true as in the example, the following expression will be executed, which will print a message. Note that the indentation here is just to increase readability, and if you want, you can write all the statements in one line.
The style of this bracket-intensive (parenthesis-heavy) used in Lisp code is also known as the "s-expression (s-expressions)." It can also be used as a common way to define structured data, just like XML. Lisp has a lot of built-in functions, and you can easily manipulate the data using the S expression format, which in turn facilitates another powerful advantage of Lisp: Since the syntax is so simple, writing programs that produce and modify code is much simpler than other languages. When we demonstrate the example of a macro (macros), we learn more about the situation.