SMAS 2.0b

  A Primer

   

  5. LISP Code for the Examples (Skills)

Creating the Skills

Simple skills

More complex skills

(defun static-dumb-factorial (agent environment nn)
  "this function is called when a factorial agent receives a request ~
   to prcess a number. If the number is less than or equal to 2, then ~
   the result is 1 and is returned right away. If the number is ~
   greatr than 2, then the agent creates a subtask, subcontracting ~
   the agent MUL-1 (presumably a friend of its) for performing the ~
   multiply operation. At the same time it records in its memory ~
   where it stands, i.e., the value of the next number to process."
  (if (< nn 2) (static-exit agent 1)
      (let (env subtask)
        ;; create a subtask for computing the product of 
        ;; the first two top values asking agent MUL-1 to compute
        (send-subtask agent :to 'MUL-1 :action 'multiply 
                      :args (list nn (1- nn)))
        ;; define the :n tag in the environment to record the value 
        ;; of the next products to compute. e.g., nn-2 -> (nn - 2)!
        (setq env (append (list :n (- nn 2)) environment))
        ;; record environment
        (update-environment agent env)
        ;; quit
        (static-exit agent :done))))
(defun dynamic-dumb-factorial (agent environment answer)
  "this function is called whenever we get a result from a subtask. This ~
   approach is not particularly clever, since the computation is linear ~
   and uses the same multiplying agent."
  (let ((nn (cadr (member :n environment)))
        subtask)
    ;; if the recorded value is 1 or less, then we are through
    (if (< nn 2)
      ;; thus we do a final exit
      (dynamic-exit agent answer)
      ;; otherwise we multiply the answer with the next high number
      ;; creating a subtask
      (progn
        (setq subtask (create-subtask agent))
        (send-subtask agent subtask :to 'MUL-1 :action 'multiply
                      :args (list answer nn))
        ;; update environment
        (setf (cadr (member :n environment)) (1- nn))
        (update-environment agent environment)
        answer
        ))))


Copyrignt Jean-Paul Barthès@UTC, 1998 Last update, Jan 99