Lisp case with different equality predicate

  • A+
Category:Languages

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.

 


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

(switch (combination :test #'string=)   ("FOO" …)   …) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: