Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Lisp and Java

Lisp and Java In this article, we\'re going to steal an idea from one of the most theft-worthy languages out there: Lisp. We\'re going to pick out one of its most useful features -- the ability to treat functions as data -- and talk about how to apply th

Tutorial Details:

Lisp Code and How To Read It

(this (is what)
(lisp code
(looks)
(like (more (or less)))))

A casual observer of Lisp code will notice the dazzling collection of parentheses, without a heck of a lot else to visually break up the code. Where in a language with C-descended syntax you get parentheses, curly braces, commas, colons, semicolons, and a whole horde of other syntactical signposts to keep you on your way, in Lisp, you get pretty much nothing but ( and its friend ). All other tokens are separated by white space, and indentation, though extremely important culturally, has no significance as part of the language itself.

For example, in Lisp, if you want to, say, call a function that doubles a number, you write:

(double 7)

Side note: when discussing Lisp, it\'s common to show the result of evaluating an expression as follows:

(double 7)
=> 14

Which can be read as \"(double 7) evaluates to 14.\"

In Java, there are basically two ways you can call a function: as an instance method or as a class method. For the former, you\'d have something like:

aNumberSeven.double()

For the latter:

aClass.double(7)

For both of these cases in Java, you learn to see the word just before the opening parenthesis as \"hot\" -- it\'s the verb of the phrase you\'re reading. One nice thing about Java\'s instance method-call syntax is that makes it very clear what the subject of that verb is (by matching the English language\'s subject-verb-object order). In Lisp, the word immediately after the opening parenthesis is what you learn to read as the verb (in most cases), and there is no distinguished subject of that verb. This pattern is used for functions that are infix operators in other languages, such as plus and minus:

(+ 5 2)
=> 7

Or assignment (which has an undefined value, so we won\'t show it evaluating to anything):

(define a 11)

which is more or less equivalent to:

int a = 11;

Note that Lisp doesn\'t specify the type of variable a -- in Lisp, a variable can hold any type.

The intense regularity to the syntax can be forbidding. Parenthetical digression: the payoff comes through the ability to represent Lisp programs as Lisp data, which means that Lisp programmers can easily write programs to manipulate other programs. Although that is indeed a trick worth stealing or even spending a lifetime studying, it would take more of a book than an article to explore it fully. For such a book, check out Paul Graham\'s ANSI Common Lisp or On Lisp (available as a PDF on his web site), which contain many inspiring uses of the mighty Lisp macro. End parenthetical digression.

Moving on through the rudiments of the language: the basic data structure in Lisp is the list, which can be created in a variety of ways, one of the simplest of which is via the list function.

(define lst (list 2 3 5 7 11))

lst => (2 3 5 7 11)


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Lisp and Java

View Tutorial:
Lisp and Java

Related Tutorials:

Java in a Nutshell Code Example
The Java programming examples shown here are from the book Java in a Nutshell , by David Flanagan, published by O\'Reilly & Associates.
 
Scripting power saves the day for your Java apps
Scripting power saves the day for your Java apps
 
JavaWorld - Java Tips index
JavaWorld - Java Tips index
 
Web services hits the Java scene, Part 1
Web services hits the Java scene, Part 1
 
Will Big Blue eclipse the Java tools market?
Will Big Blue eclipse the Java tools market?
 
How to build an interpreter in Java, Part 1: The BASICs (JavaWorld / May 1997 / by Chuck McManis)
How to build an interpreter in Java, Part 1: The BASICs (JavaWorld / May 1997 / by Chuck McManis)
 
Build an interpreter in Java -- Implement the execution engine (JavaWorld / July 1997 / by Chuck McManis)
Build an interpreter in Java -- Implement the execution engine (JavaWorld / July 1997 / by Chuck McManis)
 
Interface Tool for Java
Interface Tool for Java Interface Tool for Java is a tool that allows Java programs to communicate with ActiveX objects. It allows easy integration of ActiveX objects into a Java Environment
 
Lisp and Java
Lisp and Java In this article, we\'re going to steal an idea from one of the most theft-worthy languages out there: Lisp. We\'re going to pick out one of its most useful features -- the ability to treat functions as data -- and talk about how to apply th
 
Java 2D imaging for the Standard Widget Toolkit
Java 2D imaging for the Standard Widget Toolkit Bring the power of 2D imaging to your Eclipse plug-ins In this article, however, you'll learn how to have the best of both worlds. I'll demonstrate a simple technique that will allow you to paint Java
 
Java POS Software
Java POS Software Large retailers are increasingly turning to Java software to manage their retail enterprise.
 
Java Mime Magic Library
Java Mime Magic Library jMimeMagic is a Java library for determining the MIME type of files or streams.
 
JLisa - A Rule Engine for Java
JLisa is a powerful framework for building business rules accessible to Java and it is compatible with JSR94 V, the JavaTM Rule Engine API JLisa is more powerful than Clips because it has the expanded benefit of having all the features from common lisp a
 
Core Java Data Objects Excerpt
This book excerpt is from Core Java Data Objects,
 
Object-Oriented Language: Java / APIs (Classes & Libraries)
The Java Platform APIs are a set of essential interfaces that developers need to build their Java applications and applets. All Java Platfrom APIs are open and extensible, and are created by JavaSoft and industry-wide specialists in each target technology
 
Tiger and Beyond, the Future of the Java Platform
Part Two of an interview with Sun Microsystems' Sun Fellow, Graham Hamilton, explores Java 2 Platform, Standard Edition 5.0 (J2SE 5.0) and the future of the Java language.
 
Welcome to Java Developers paradise!
Welcome to Java Developers paradise! T his site contains many quality Java, JSP, RMI, MySQL downloads, tutorials, source codes and links to other java resources. We have large number of links to the tutorials on java which will help you learn java
 
Collection of Large Number of Java Interview Questions!
Collection of Large Number of Java Interview Questions! Collection of Large Number of Java Interview Questions The Core Java Interview Questions The Jakarta Struts Interview Questions
 
Collection of Large Number of Java Sample Programs and Tutorials
Collection of Large Number of Java Sample Programs and Tutorials Collection of Large Number of Java Sample Programs and Tutorials HelloWorld Java Program Simple Java Program for beginners that prints HelloWorld! on console.
 
JPackIt JPackIt is a Java application for packaging a Java project into single executable package
Java Project in single jar, class or exe containing all java application resources and referenced libraries.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.