CLIPS - 如果那么其他函数给[CSTRCPSR1]错误

时间:2016-05-28 14:06:09

标签: if-statement clips

以下是错误摘要:

CLIPS> (load "C:/Users/labor/Desktop/Witek/projekt.CLP")
Defining defrule: R1 +j+j
Defining defrule: R2 +j+j
Defining defrule: R3 =j+j+j
Defining defrule: imie-if =j=j+j+j

[CSTRCPSR1] Expected the beginning of a construct.

这是我的CLIPS程序的代码。基本上,如果名字和姓氏与亚伯拉罕·林肯不同,我希望它的反应不同。

(defrule R1
(initial-fact)
=>
(printout t "Enter your name:" crlf)
(bind ?name (read))
(assert (name ?name)))

(defrule R2
(name ?name)
=>
(printout t "Enter your last name" crlf)
(bind ?lastnm (read))
(assert (lastnm ?lastnm)))

(defrule R3
(and(name ?name)(lastnm ?lastnm))
=>
(printout t "How old are you " ?name "?" crlf)
(bind ?age (read))
(assert (age ?age)))

(defrule name-if
(name ?name)(lastnm ?lastnm)(age ?age)
=>
(if(and(eq ?name Abraham)(eq ?lastnm Lincoln))
then (printout t "Hello " ?name " " ?lastnm ", you are " ?age " years old bro" crlf))
else (printout t "Hello " ?name " " ?lastnm ", you are " ?age " years old" crlf)))

我从某个网页复制了if语句逻辑,我不太清楚,在这种情况下,'eq'代表什么......如果你能另外解释它的作用,我会很感激。

此致 w ^

1 个答案:

答案 0 :(得分:1)

在then子句的末尾有一个额外的右括号导致问题。 Mac OS和Window CLIPS IDE具有一个balance命令,您可以使用该命令查看括号是否在构造中正确平衡。只需在构造内部单击并多次应用balance命令,直到选中整个构造。如果您将光标放在then关键字和balance中,您将看到if语句由then子句末尾的括号关闭,而else子句保持悬空。

enter image description here

更正的规则是:

(defrule name-if
   (name ?name)
   (lastnm ?lastnm)
   (age ?age)
   =>
   (if (and (eq ?name Abraham)
            (eq ?lastnm Lincoln))
      then 
      (printout t "Hello" ?name " " ?lastnm ", you are " ?age " years old bro" crlf)
      else 
      (printout t "Hello " ?name " " ?lastnm ", you are " ?age " years old" crlf)))

eq谓词是equals的缩写。与期望数字参数的=谓词不同,eq比较任何类型的值。