English 中文(简体)
Lisp code called from Java
原标题:

Long story:

I am doing a project for my functional programing class, and I thought of writing an AI controller in Lisp, for the Mario AI competition.

I was looking over frameworks/libraries/ways of calling Lisp code from Java, or even better Lisp-Java intercommunication.

I have looked at Jacol, but it is old, and it does not compile fine for me.

My best choice so far is: Jatha. It is really neat, although some lisp constructs are not yet implemented, one can easily define his own constructs. For example mapcar and cond are not implemented. I have implemented my own mapcar, named mapp in Lisp like this:

(defun map* (f l r) 
  (if (null l)
      r
      (map* f (rest l) (cons (funcall f (first l)) r))))

(defun mapp (f l)
    (reverse (map* f l nil)))

Now I have a simple function that uses this, for example, a function that numbers how many atoms there are in a non-linear list

(defun myfunc (l)
  (if (atom l)
       1
      (apply  + (mapp  myfunc l)))) 

(myfunc  (6 2))

This all works fine in clisp Now to call Lisp code from Java I used Jatha. All one has to do is import the Jatha library in the java project and load a lisp file like this (ex):

import org.jatha.*;
import org.jatha.dynatype.*;

public class Main {

    public static void main(String[] args) {
        Jatha lisp = new Jatha(false, false);
        lisp.init();
        lisp.start();
        LispValue file = lisp.makeString("X:\lispproject\test1.lisp");
        LispValue rez1 = lisp.load(file);
    }
}

While that code works fine in clisp, and other implementations, this code produces a StackOverflow

run: APPLY: fn = +, args = ((1 1)) S: ((+ (QUOTE 1) (QUOTE 1))) Exception in thread "main" java.lang.StackOverflowError at java.lang.Long.toString(Long.java:242) at java.lang.Long.toString(Long.java:100) at java.lang.String.valueOf(String.java:2946) at org.jatha.dynatype.StandardLispInteger.toString(StandardLispInteger.java:113) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152) at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153) at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153) at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152) at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152) at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152) at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152) at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152) at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152) at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153) at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152) at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:17

So my question is, why does it do this? Is my code wrong?
Is it a bug in Jatha? See for yourself, it doesn t take long to set up.
Have you ever done something similar?
Do you know any other better ways to do this? All I want is to call from Java some Lisp code, get it executed, and get back results computed by the Lisp code.
Thanks.

[Edit] Fixed code, pasted something wrong.

最佳回答

ABCL is a Common Lisp implementation that runs directly in the JVM - possibly it is the best option to run Common Lisp side by side with Java.

问题回答

Have you looked into Clojure? It s a Lisp dialect that runs on the Java Virtual Machine.

FOIL (Foreign Object Interface for Lisp) provides means to interact with Java and .NET programs.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签