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 programming language in which are still used today. It is values of symbols. Programmers don’t 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 on LISP. modified in OOP? You don’t have to change compiler 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 anonymous function 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 Common LISP • 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. C++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