Functions
- A function is a body of code that gets parameters.
- Depending on the parameters the function may calculate a return
value and may have side effects
- A simple version of this can be done by making rules that
only run when a particular fact is set.
- When the function is finished, that fact is changed.
- To call the function, you need to set up the parameters
and then set the fact.
- This is only a simple version of function calls, because it
does not allow recursive functions.
- For example: (defrule start-loopfunc (start myfunc) =>
(assert (value 1))
(assert (in myfunc)))
- (defrule loopfunc (in myfunc) (value ?a) (test (< ?a 5)) =>
(assert (value (+ ?a 1))))
- (defrule endfunc ?f1 <- (in myfunc) (value ?a) (test (= ?a 5)) =>
(retract ?f1))