Re: JESS: Possible bug: using apply with str-cat inside a function

2005-12-02 Thread ejfried
Jess's "apply" differs from the classic Lisp "apply" in that the
varargs are in the "apply" funcall, rather than being bundled up in a
list. This is by accident rather than by design, I'm afraid. In
classic Lisp, this form

(apply str-cat "foo" " bar" " baz")

is an error, but in Jess, it's correct. In classic Lisp, you'd have
to say something like

(apply 'str-cat (list "foo" " bar" " baz")). 

to get the same effect.


I think Steve Solomon wrote:
> Jess> (apply str-cat "foo" " bar" " baz")
> "foo bar baz"

Here there are three arguments to str-cat; each is converted into a
String, the strings concatenated, and the result is returned as a
String.

> 
> Jess> (deffunction test ($?args) (apply str-cat ?args))
> Jess> (test "foo" " bar" " baz")
> "\"foo\" \" bar\" \" baz\""

Here, on the other hand, there is just one, the list in ?args. That
list is converted to a String, and that String is returned. 

> To concatenate strings inside a function currently I use a foreach loop:
> (bind ?res "")
> (foreach ?str ?args (bind ?res (str-cat ?res ?str)))
> where ?args is the multifield parameter of the function as above.
  
Here's an (untested, but probably works) "apply*" function which acts
like classic Lisp "apply". Feel free to use it:

import jess.*;
public class ApplyStar implements Userfunction, Serializable {
public String getName() { return "apply*"; }
public Value call(ValueVector vv, Context c) {
Funcall f = new Funcall(vv.get(1).stringValue(c), c.getEngine());
ValueVector args = vv.get(2).listValue(c);
for (int i = 0; i < args.size(); i++)
f.arg(args.get(i));
return f.execute(c);
}
}

-
Ernest Friedman-Hill  
Advanced Software Research  Phone: (925) 294-2154
Sandia National LabsFAX:   (925) 294-2234
PO Box 969, MS 9012 [EMAIL PROTECTED]
Livermore, CA 94550 http://herzberg.ca.sandia.gov


To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]




JESS: Possible bug: using apply with str-cat inside a function

2005-12-02 Thread Steve Solomon








If you call apply with str-cat outside of a function, the
result is what you would expect: a single string that is the argument strings
concatenated.

 

Jess> (apply str-cat “foo” “ bar”
“ baz”)

“foo bar baz”

 

If you declare a function that performs the concatenation on
its single multifield argument, the result of the apply inside the function is
a string with the original quotes preserved:

 

Jess> (deffunction test ($?args) (apply str-cat ?args))

TRUE

Jess> (test "foo" " bar" " baz")

"\"foo\" \" bar\" \" baz\""

 

To concatenate strings inside a function currently I use a foreach
loop:

 

(bind ?res "")

(foreach ?str ?args (bind ?res (str-cat ?res ?str)))

 

where ?args is the multifield parameter of the function as
above.

 

Bug? Or am I not understanding something?

 

Thanks,

 

Steve Solomon

Institute for Creative Technologies

University of Southern
  California

solomon “at” ict.usc.edu