Search code examples
if-statementexceptionclips

CLIPS - How can I exclude characters from IF/READ statement's exceptions?


The code is asking a y/n question to the user and making a change - simple. The statement seems to accept only integer and float types and I need only two answers, so I used 1 and 0, and excluded the rest, BUT it reads only numbers, so only the numbers are excluded, not characters.

(defrule rule01
    =>
    (printout t "Question (yes=1/no=0)?" crlf)
    (bind ?x (read))
    (if (!= ?x 1)
        then
        (if (= ?x 0)
            then
            (assert (rule01 no))
        else (printout t "Use ONLY 0 OR 1 for your answers!" crlf))
    else (assert (rule01 yes))))

Currently, when you try to type in a character, it returns the following:

CLIPS> (run)
Question (yes=1/no=0)?
g
[ARGACCES5] Function <> expected argument #1 to be of type integer or float
[PRCCODE4] Execution halted during the actions of defrule rule01.

How can I put in an exception for characters?


Solution

  • Use eq and neq in place of = and <>.

             CLIPS (6.31 6/12/19)
    CLIPS> 
    (defrule rule01
        =>
        (printout t "Question (yes=1/no=0)?" crlf)
        (bind ?x (read))
        (if (neq ?x 1)
            then
            (if (eq ?x 0)
                then
                (assert (rule01 no))
            else (printout t "Use ONLY 0 OR 1 for your answers!" crlf))
        else (assert (rule01 yes))))
    CLIPS> (reset)
    CLIPS> (run)
    Question (yes=1/no=0)?
    g
    Use ONLY 0 OR 1 for your answers!
    CLIPS>