Jess71p2 26 - function will receive when it is called...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
4. Defining Functions in Jess 18 executes before (and thus can alter the argument list seen by the real function, or short-circuit it completely by returning a value of its own) or after the real function (and thus can see the return value of the real function and possibly alter it. ) defadvice provides a great way for Jess add-on authors to extend Jess without needing to change any internal code. Here are some examples of what defadvice looks like. This intercepts calls to 'plus' (+) and adds the extra argument '1', such that (+ 2 2) becomes (+ 2 2 1) -> 5. The variable '$?argv' is special. It always refers to the list of arguments the real Jess
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: function will receive when it is called. Jess> (defadvice before + (bind $?argv (create$ $?argv 1))) TRUE Jess> (+ 2 2) 5 This makes all additions equal to 1. By returning, the defadvice keeps the real function from ever being called. Jess> (defadvice before + (return 1)) TRUE Jess> (+ 2 2) 1 This subtracts one from the return value of the + function. ?retval is another magic variable - it's the value the real function returned. When we're done, we remove the advice with undefadvice . Jess> (defadvice after + (return (- ?retval 1))) TRUE Jess> (+ 2 2) 3 Jess> (undefadvice +) Jess> (+ 2 2) 4...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online