This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Compiling procedures questions


Mayer,

I think your TEST_RULE variable should read:

    private static final String TEST_RULE = "(or (proc1?) (or (proc2?)
(proc3?)))";
 
Note the parentheses around proc1?, proc2? and proc3?. Since each of these
variables is bound to a procedure, which evaluates to itself and is
considered a true value (the 'or' special form evaluates each argument in
turn and returns the first value that is not false), the result of

(or proc1? (or proc2? proc3?))    ;; which can be written (or proc1? proc2?
proc3?)

will return the value bound to proc1?, a procedure.

Dominique Boucher

> =====================================================================
> ////////////////////////////////////////////////////////
> // RandomKawaProc.java
> ////////////////////////////////////////////////////////
> 
> import gnu.mapping.Procedure0;
> 
> import java.util.Random;
> 
> public class RandomKawaProc extends Procedure0 {
>     private Random random = new Random();
> 
>     public RandomKawaProc( String name )  {
>         super( name );
>     }
> 
>     public Object apply0() throws Throwable {
>         if ( random.nextDouble() > 0.5 )  {
>             return Boolean.TRUE;
>         }
> 
>         return Boolean.FALSE;
>     }
> }
> ======================================================================
> 
> 
> ======================================================================
> ////////////////////////////////////////////////////////
> // KawaTestProcs.java
> ////////////////////////////////////////////////////////
> 
> public class KawaTestProcs {
>     public static final RandomKawaProc testProc1 = new RandomKawaProc( 
> "proc1?" );
>     public static final RandomKawaProc testProc2 = new RandomKawaProc( 
> "proc2?" );
>     public static final RandomKawaProc testProc3 = new RandomKawaProc( 
> "proc3?" ); } 
> ======================================================================
> 
> 
> ======================================================================
> ////////////////////////////////////////////////////////
> // KawaTest.java
> ////////////////////////////////////////////////////////
> 
> import gnu.mapping.Procedure;
> import kawa.standard.Scheme;
> 
> public class KawaTest {
>     private static final String TEST_RULE = "(or proc1? (or proc2?
> proc3?))";
> 
>     public static void main( String[] args )  {
>         try  {
>             Procedure proc = compileRule( TEST_RULE );
>             System.out.println( "Procedure value = " +
> proc.apply0() );
>         }
>         catch( Throwable t )  {
>             t.printStackTrace();
>             System.exit( 1 );
>         }
> 
>         System.exit( 0 );
>     }
> 
>     /**
>      * A helper method to compile a String rule into a Procedure.
>      *
>      * @param rule The string representation of the rule
>      */
>     private static final Procedure compileRule( String rule ) throws 
> Throwable {
>         Scheme scheme = new Scheme( Scheme.builtin() );
>         scheme.eval( "(require <" +
> KawaTestProcs.class.getName() + ">)" );
>         String body = "( define (rule) " + rule + ")";
>         scheme.eval( body );
>         return (Procedure)scheme.eval( "rule" );
>     }
> }
> ======================================================================
> 
> 
> When I run the main of KawaTest the output of the println statement 
> is:
> 
> 	Procedure value = #<procedure proc1?>
> 
> What I was trying to do though was to get the result as true or false.
> 
> 
> Thanks,
> Mayer
> 
> 
> -----Original Message-----
> From: Per Bothner [mailto:per@bothner.com]
> Sent: Tuesday, September 28, 2004 1:51 AM
> To: Crystal, Mayer
> Cc: 'kawa@sources.redhat.com'
> Subject: Re: Compiling procedures questions
> 
> 
> Crystal, Mayer wrote:
> 
> > In other words, my code looks something like:
> > 
> > 1.  String rule =  "(or proc1? (or proc2? proc3?))";
> 
> I'd recommend wrapping the rule in a function.
> Scheme doesn't have the concept of evaluating a module - e.g. "load" 
> returns an undefined result.  Yes, there are ways in Kawa, but they're 
> a bit more tricky, as the default behavior is to ignore the values of 
> top-level expressions.
> 
> String body = "(define (rule) (or ...))";
> 
> > 2.  Procedure moduleBody = compileRule( rule ); 3.  return 
> > ((Boolean)moduleBody.apply0()).booleanValue();
> 
> ;; Get the "rule" function in the Scheme global environment: 
> Procedure rule
> = scheme.eval("rule");
> 
> (Boolean) rule.apply0()
> -- 
> 	--Per Bothner
> per@bothner.com   http://per.bothner.com/
> 



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]