Compare LISP with Object-Oriented Programming Languages \ PENGFEI WANG Department of Computer Science, Furman University, Greenville, SC

Introduction Advantages of LISP Disadvantages of LISP

• LISP is a family of functional programming languages 1. Variables are Immutable 1. Hard to learn specified in 1958. It is the second-oldest high-level 1. Easy to test: functions CANNOT change any 2. No deadlock can occur: LISP • LISP was originally created as a mathematical notation in which are still used today. It is values of symbols. Programmers don’ have to programs can be run in parallel for computer programs. LISP programmers have to learn very different from Object-Oriented Languages and worry about changing in values of the variables. If without deadlock. It actually doesn’t Lambda Calculus. error occurs, they only have to trace the wrong doesn’t have much popularity as Object-Oriented need a “lock” on variables because it • Therefore, it is nearly impossible for newbies to master return value from one function to another. This Languages do, but, besides its wide use in Artificial will never change its value (typically). LISP, but it is relatively much easier to master an OOL. feature significantly decrease the amount of • On multi-thread programs, LISP is Intelligence, it is also a good general purpose • Furthermore, due to FP and OOP’s differences, it is work on debugging. more efficient than OOP, because highly difficult for programmers to transfer from OOP programming language as well. • How does LISP realize the same function as it can be run in parallel. Even if the languages (e.g. JAVA) to FP languages (e.g. LISP). • This paper is going to discuss the differences between OOP, when values are “expected” to be program is single-thread, the 2. Small Libraries LISP and OOP Languages and focus mainly . modified in OOP? You don’t have to change can make the program • Because of the hardness of learning and transferring from • “LISP” stands for LISt Processor. any values or create variables in FP (typically). run in parallel on CPU. • Higher-order functions: functions that take • (someFunction1 x) OOP to FP, the programmer community is very small. functions as inputs or returns (outputs). • (someFunction2 y) • Popularity: JAVA vs LISP Attention: Abbreviations • Example 1: function as a input • (someFunction3 x y) • Google Search: See Figure 3 • (/ 1 (+ 2 3)) ; addition is the input function • These functions can be run in • Amazon Books: 27486 (JAVA) vs 1286 (LISP) In this article, FP and OOP are abbreviations of • Example 2: function as a output parallel because the first two • Libraries: 4024 (JAVA7) vs 978 (LISP) Functional Programming and Object-Oriented • (defun test (a) #’ (lambda (b) (+ b a))) functions cannot change the value • This is essential, because productivity matters in • ;; “test” returns an Programming respectively. of x and y (typically). An OOP companies. Programmers have to write functions by (lambda), which does addition (+ b a) compiler, however, will worry themselves, which are already existed in JAVA API.

• Actually, you can assign a value to a variable about whether x and y’s values are Searchtimes and change the value of the variable. However, Figure 3: Popularity. changed in the previous functions Source: Google Trend LISP: “Hello, world!” these functions are highly discouraged, which and therefore someFunction3 cause unexpected errors. These functions are should be run at last. This is also a called destructive functions. A destructive example of why destructive function normally has a safe version functions are not recommended. (delete→remove). The safe version returns a Destructive functions might new data structure instead of changing it. change the values and cause unexpected errors. Case Study: Bubble Sort 2. Closure • Based on high-order functions, LISP can actually do what OOP languages can hardly • do: closure. Closure is needed when a function tries to access a captured variable from Data Set: 1000 Numbers List; Run 100 times outside. • Environment: Windows 10; CPU @ 2.90GHz (8 CPUs) • (defun addition (n) #'(lambda (x) (+ x n))) ; Example of closure in LISP • Compiler: JAVA 1.8.0; Steel Bank • This function takes a input n and returns a function which adds its input, x, to n. • In order to save space, JAVA source code do not show up • When a function (i.e. lambda (x)) refers to a variable outside of it (i.e. n), this here. The algorithm comes from WikiBooks. function is called closure. The function need to be defined in the same environment with its free variable (i.e. n). 1. Function is the basic component of Functional • Actually some OOP languages start to implement closure based on the idea from Programming languages. Function can be a input of a functional languages, but LISP’s closure is far more concise than those (e.g. ++11). function or a output of a function. Therefore, recursion is 3. Extendable Language: Macro very common in LISP. • LISP can be extended by itself, using Macro, which changes the behavior of a compiler 2. Common structure of the language: List. Even functions during compiling. By macro, you can simulate any grammar you like, for example the (the basic component) are expressed by lists! In this whole JAVA. Theoretically, with macro, LISP is equivalent to Lambda Calculus. Common LISP Java program, “(format )” is a • (defun simple-reader-extension (input1 input2) (…) (do something) ) ; Example Lines 6 14 function expressed by the list. The first one (“format”) in • (set-macro-character #\[ ‘simple-reader-extension) ; Example Characters 142 282 the list is the name of the function, and the following • The program is going to execute simple-reader-extension whenever it sees a “[“. The Run-Time 208ms 16ms elements (“t” and “’Hello, world!”) are inputs. substitution (macro expansion) is done during compilation. Note: Comments are not counted. 3. Interactive Testing: classical LISP are 4. Fast & Concise • Analysis: We can see that Common LISP is so concise but • interactive. After you finish one line of code, the claims that LISP is “a language for writing fast programs and a its speed is not ideal, comparing to the experiment in language for writing programs fast”. compiler (and interpreter) will execute that line of code Figure 2 (< Lisp as an Alternative to Java). Why? This is a • Yes, LISP is concise. Actually, it is one of the most concise language being widely and then you can write the second line. Actually, you can used up till now. Also, its run-time is proved to be faster than others, even good example of why optimization is necessary. test every function independently in one program in comparing to C/C++. However, in order to make LISP fast, the program and the 1. Accessing an array in JAVA is O(1), but accessing a list in some IDE. The execution step is called evaluation in FP. compiler must obey to strict optimization and FP rules. LISP is O(n). Figure 2: LISP is fast 2. This Common LISP program uses a destructive function Actually, Common Lisp, as one of the most popular Figure 1: LISP is concise. Source: RedMonk (Note: LISP is a FAMILY of languages) called “rotatef”. Using imperative style in LISP (e.g. dialect of LISP, is not purely functional. Although rotatef), instead of FP style, will make it super slow. programmers are recommended to write in • How to optimize? functional way, the language itself is multi- 1. Optimize the Algorithm for FP languages. paradigm, which also allows object-oriented 2. Choose a better dialect of LISP with better optimization. programming (which is often procedural as well). For example, newLISP can access a list by O(1).