Search code examples
clips

How can I compare a variable that is passed to a function with a string in CLIPS?


When I tried to compare a variable with a string, it gives me an error. I tried to compare it with (= ?a "s").

This is the full code example that produces the error:

(deffunction cierto (?a)
  (if (= ?a "s")
    then
      (printout t TRUE crlf)
    else
      (printout t FALSE crlf)
  )
)

The error:

Defining deffunction: cierto
[ARGACCES5] Function = expected argument #2 to be of type integer or float

ERROR:
(deffunction MAIN::cierto
   (?a)
   (if (= ?a "s")
FALSE

Solution

  • (deffunction cierto (?a)
      (if (eq ?a "s")
        then
          (printout t TRUE crlf)
        else
          (printout t FALSE crlf)
      )
    )
    

    (= ) is for comparing numbers (INTEGER or FLOAT) for equality.

    • (= 3 3.0) is TRUE
    • (= 3 3) is TRUE
    • (= s s) ERROR, s is not a NUMBER

    (eq ) is for comparison of PRIMITIVE values and also comparison of types)

    • (eq 6 6.0) is FALSE, different types (INTEGER vs. FLOAT)
    • (eq 6 6) is TRUE, same type same value (INTEGER INTEGER)
    • (eq si "si") is FALSE, different types (SYMBOL vs. STRING)
    • (eq si si) is TRUE, same type, same values (SYMBOL SYMBOL)
    • (eq "si" "si") is TRUE, same type same values (STRING STRING)

    More information in the Basic Programming Guide (Section 12: Actions And Functions)

    Alternative:

    (deffunction cierto2 (?a)
      (printout t (eq ?a "s") crlf)
    )
    

    You cold also use a SYMBOL s instead of a STRING "s".