Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jason.stdlib;
  
 

Internal action: .min.

Description: gets the minimum value of a list of terms, using the "natural" order of terms. Between different types of terms, the following order is used:
numbers < atoms < structures < lists

Parameters:

  • + list (list): the list where to find the minimal term.
  • +/- minimal (term).

Examples:

  • .min([c,a,b],X): X unifies with a.
  • .min([b,c,10,g,f(10),[3,4],5,[3,10],f(4)],X): X unifies with 5.
  • .min([3,2,5],2): true.
  • .min([3,2,5],5): false.
  • .min([],X): false.

 
 public class min extends DefaultInternalAction {
     
     private static InternalAction singleton = null;
     public static InternalAction create() {
         if ( == null
              = new min();
         return ;
     }
 
     @Override public int getMinArgs() { return 2; }
     @Override public int getMaxArgs() { return 2; }
 
     @Override protected void checkArguments(Term[] argsthrows JasonException {
         super.checkArguments(args); // check number of arguments
         if (!args[0].isList())
             throw JasonException.createWrongArgument(this,"first argument must be a list");
     }
 
     @Override
     public Object execute(TransitionSystem tsUnifier unTerm[] argsthrows Exception {
         checkArguments(args);
 
         ListTerm list = (ListTerm)args[0];
         if (list.isEmpty()) {
             return false;               
         }
 
         Iterator<Termi = list.iterator();
         Term min = i.next();
         while (i.hasNext()) {
             Term t = i.next();
             if (compare(min,t)) {
                 min = t;
             }
         }
         return un.unifies(args[1], min.clone());
     }
     
    protected boolean compare(Term aTerm t) {
        return a.compareTo(t) > 0;
    }
New to GrepCode? Check out our FAQ X