剪辑将多值变量传递给deffunction

问题描述 投票:-1回答:1

我目前正在编写一个模拟命题法的程序。我收到了一个函数my-test,以确定某些字符是否在字符串中,如果我只传递一个插槽变量但我赢了#t接受多时隙/多值变量,我工作正常。

我已经将代码用于传递,例如单个但是如果我尝试将$?符号传递给程序,则表示它需要一个字符串或符号。

(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'.

这是我运行程序时收到的错误我觉得我需要进行转换但不太确定需要做什么。谢谢你的帮助

rules clips
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.