3

I am searching the API for handling the values in Armed Bear Common Lisp (ABCL) implementation of the Common Lisp language in the JVM, using Java.

It works when a function returns (only) a list or a string.

When it returns multiple values I only can fetch the first returned value.

I do not know how to fetch the other values.

This is my test.lisp file :

(defun get-list ()
   (list "abc" 12 'a 'b))

(defun get-value ()
    (values "abc" 12 'a 'b))

And my Java code is :

public static void main(String[] args) throws Exception {
    Interpreter interpreter = Interpreter.createInstance();

    LispObject lobj = interpreter.eval("(load \"test.lisp\")");
    org.armedbear.lisp.Package defaultPackage = Packages.findPackage("CL-USER");

    Symbol myFunctionSym = defaultPackage.findAccessibleSymbol("GET-LIST");
    Function myFunction = (Function) myFunctionSym.getSymbolFunction();
    LispObject o = myFunction.execute();

    System.out.println(o.listp());    // this return false

    Symbol myFunctionSym2 = defaultPackage.findAccessibleSymbol("GET-VALUE");
    Function myFunction2 = (Function) myFunctionSym.getSymbolFunction();

    LispObject o2 = myFunction.execute();

}
savior
  • 802
  • 6
  • 14
  • 3
    One of the reasons for closing questions it that "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance." Can you show the code that you're having trouble with, and what it is you're trying to do? As one answer has already pointed out, if this is all in the Lisp side, it may be as simple as `multiple-value-bind`, e.g., `(multiple-value-bind (quotient remainder) (floor 3/2) (list quotient remainder)) => (1 1/2)`. – Joshua Taylor Aug 27 '13 at 13:48
  • I think it's too early to raise the spectre of closing the question. Everyone makes mistakes in formulating questions at first. (Historically, the CL community has had a reputation for being harsh to newbies, but SE doesn't.) – Mars Aug 27 '13 at 18:54
  • @mars Agreed; while I posted text from the close reasons, I didn't cast a close vote at the time (and haven, because it makes sense to give the OP some time to edit. A few days have passed now, with no edit from the OP... How long – Joshua Taylor Aug 29 '13 at 03:17
  • @Mars Agreed; I didn't cast a close vote when I copied the closing text, because I wanted to give the OP some time to update. It's been a few days now and there are still no updates. I haven't voted to close yet, but unless some more details come eventually, there's not enough information to answer this question. – Joshua Taylor Aug 29 '13 at 03:19
  • I understand. Person might be busy at work or home life, or might have just drifted away for good. – Mars Aug 30 '13 at 02:51

4 Answers4

2

Is multiple-value-bind or nth-value what you are looking for, or is there something more to this question?

Svante
  • 50,694
  • 11
  • 78
  • 122
  • 1
    I think savior wants to do it from the Java side--i.e. s/he has figured out how to access the first value in a purely Java way, and wants to do the same for the additional values when there are multiple values. But it seems as if s/he could do that by assigning to separate variables in Lisp, and then use whatever Java technique s/he's already using to get to those variables' values. I agree with @JoshuaTaylor that it would help to see what's already been done. – Mars Aug 27 '13 at 18:49
0

You could use multiple-value-bind, etc. on the Lisp side to assign to separate variables, and then access them individually in Java. Or you could call multiple-value-bind from Java. That's not the answer you are looking for. I assume what you'd really like is a method that you can call in Java that will get values other than the first one directly. I don't have that answer, but no one else has provided that answer.

However, poking around in the source and in javadoc, I see that classes Primitives and Symbol have methods VALUES and MULTIPLE_VALUE_BIND. I would guess that these are not designed to be called in user source code, but they might at least help you find the answer you want.

And finally, I think this might be something that you need to ask on the ABCL mailing list. You could report the full answer back here as an answer to your own question.

Mars
  • 8,689
  • 2
  • 42
  • 70
0

After a call to a function that returns multiple values, the values are associated with the executing LispThread until the next call into Lisp.

One may access the values object as a list of LispObject instances via a call to getValues() as evidenced by the following code:

org.armedbear.lisp.Package cl = Packages.findPackage("CL"); Symbol valuesSymbol = cl.findAccessibleSymbol("VALUES"); LispObject[] valuesArgs = { LispInteger.getInstance(1), LispInteger.getInstance(2) }; LispObject result = valuesSymbol.execute(valuesArgs); LispObject[] values = LispThread.currentThread().getValues(); for (LispObject value: values) { System.out.println("value ==> " + value.printObject()); }

easyE
  • 681
  • 1
  • 5
  • 5
0

I think there are two copy paste errors with your code above (you want to use myFunctionSym2 and myFunction2 in the last two lines, i.e.

Function myFunction2 = (Function) myFunctionSym2.getSymbolFunction();
LispObject o2 = myFunction2.execute();
System.out.println(o2.listp()); // additional line added by me

With this, it works for me.

JanDasWiesel
  • 382
  • 5
  • 14