Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lisp case with different equality predicate

As a part of a Tic Tac Toe playing bot, I need a function that evaluates combinations of tiles to points. The code would look something like this:

(case combination
    ("EEEEE" 0)
    ("EEEEP" 1)
    ("EEEPE" 1)
    ("EEEPP" 2)
    ("EEPEE" 1)
    ("EEPEP" 2)
    ("EEPPE" 2)
    ("EEPPP" 3)
    ("EPEEE" 1)
    ("EPEEP" 2)
    ("EPEPE" 2)
    ("EPEPP" 3)
    ("EPPEE" 2)
    ("EPPEP" 3)
    ("EPPPE" 3)
    ("EPPPP" 4)
    ("PEEEE" 1)
    ("PEEEP" 2)
    ("PEEPE" 2)
    ("PEEPP" 3)
    ("PEPEE" 2)
    ("PEPEP" 3)
    ("PEPPE" 3)
    ("PEPPP" 4)
    ("PPEEE" 2)
    ("PPEEP" 3)
    ("PPEPE" 3)
    ("PPEPP" 4)
    ("PPPEE" 3)
    ("PPPEP" 4)
    ("PPPPE" 4)
    ("PPPPP" 5))

(This is not the place to discuss the value of such an approach, for it is used for reasons unrelated to the question)

The problem is that case uses a predicate that doesn't return true for identical strings that aren't the same object (hard to find if it's eq or eql). How can you change that?

EDIT: I solved the original problem by converting the string into a corresponding binary number, which can be compared using eql or used as an index in a list.

like image 303
Kotlopou Avatar asked Jun 06 '26 03:06

Kotlopou


1 Answers

Use alexandria:switch from the alexandria library, available from quicklisp.

(switch (combination :test #'string=)
  ("FOO" …)
  …)
like image 166
Svante Avatar answered Jun 08 '26 23:06

Svante