剪辑将多值变量传递给函数

时间:2019-04-06 09:43:32

标签: rules clips

我目前正在编写一个模拟命题定律的程序。我已经收到一个函数my-test,用于确定字符串中是否包含某些字符,如果我仅传递一个插槽变量,但不会接受多插槽/多值变量,则可以正常工作。

我已经通过了例如?sigle的代码,但是如果我尝试将$?symbol传递给程序,它表示它正在尊重字符串或符号。

(deftemplate sentence (multislot sent))

(defrule read-from-user
=>
(bind ?response "")
(printout t "Please enter a sentence: Use ~ for not and => for implies, or(v) and and(^) please. For predicates use { and } Example exists{richard}" crlf)
(bind ?response (explode$ (readline)))
(bind ?response (replace-member$ ?response "(" (sym-cat "(")))
(bind ?response (replace-member$ ?response ")" (sym-cat ")")))
(bind ?response (replace-member$ ?response "~" (sym-cat "~")))
(bind ?response (replace-member$ ?response "v" (sym-cat "v")))
(bind ?response (replace-member$ ?response "=>" (sym-cat "=>")))
(bind ?response (replace-member$ ?response "^" (sym-cat "^")))
(bind ?response (replace-member$ ?response "[" (sym-cat "[")))
(bind ?response (replace-member$ ?response "]" (sym-cat "]")))
(bind ?response (replace-member$ ?response "{" (sym-cat "{")))
(bind ?response (replace-member$ ?response "}" (sym-cat "}")))

(assert (sentence (sent ?response))))

(deffunction my-test ($?symbol) (not (or (str-index "^" ?symbol) (str-index "v" ?symbol)))) 

(defrule negative
(sentence (sent $?before "~" "(" "~" $?symbol ")" $?after))
(test (my-test $?symbol))
 =>
 (assert (sentence (sent $?before $?symbol $?after))))

(run)
Please enter a sentence: Use ~ for not and => for implies, or(v) and and(^) please. For predicates use { and } Example exists{richard}
~(~P v Q)
[ARGACCES2] Function 'str-index' expected argument #2 to be of type symbol, string, or instance name.
[PRCCODE4] Execution halted during the actions of deffunction 'my-test'.

这是我在运行程序时收到的错误,我感觉我需要进行对话,但是不确定要做什么。感谢您的帮助

1 个答案:

答案 0 :(得分:0)

如果要将多个符号传递给my-test函数进行测试,则需要使用foreach函数对其进行迭代。

         CLIPS (6.31 4/1/19)
CLIPS> (deftemplate sentence (multislot sent))
CLIPS> 
(defrule read-from-user
   =>
   (bind ?response "")
   (printout t "Please enter a sentence: Use ~ for not and => for implies, or(v) and and(^) please. For predicates use { and } Example exists{richard}" crlf)
   (bind ?response (explode$ (readline)))
   (bind ?response (replace-member$ ?response "(" (sym-cat "(")))
   (bind ?response (replace-member$ ?response ")" (sym-cat ")")))
   (bind ?response (replace-member$ ?response "~" (sym-cat "~")))
   (bind ?response (replace-member$ ?response "v" (sym-cat "v")))
   (bind ?response (replace-member$ ?response "=>" (sym-cat "=>")))
   (bind ?response (replace-member$ ?response "^" (sym-cat "^")))
   (bind ?response (replace-member$ ?response "[" (sym-cat "[")))
   (bind ?response (replace-member$ ?response "]" (sym-cat "]")))
   (bind ?response (replace-member$ ?response "{" (sym-cat "{")))
   (bind ?response (replace-member$ ?response "}" (sym-cat "}")))
   (assert (sentence (sent ?response))))
CLIPS> 
(deffunction my-test (?symbols)
   (foreach ?s ?symbols 
      (if (or (str-index "^" ?s) (str-index "v" ?s))
         then (return FALSE)))
   (return TRUE)) 
CLIPS> 
(defrule negative
   (sentence (sent $?before "~" "(" "~" $?symbol ")" $?after))
   (test (my-test $?symbol))
    =>
    (assert (sentence (sent $?before $?symbol $?after))))
CLIPS> (run)
Please enter a sentence: Use ~ for not and => for implies, or(v) and and(^) please. For predicates use { and } Example exists{richard}
~(~P v Q)
CLIPS>