gizgok
gizgok

Reputation: 7649

How to do Pattern Matching in Common Lisp

I have no idea if there exists a pattern matching function for Common Lisp, nevertheless I have to make my own function. I have no idea about Lisp. Can somebody give heads-up on learning Lisp and most importantly, how to go about doing pattern matching in Lisp. I will have to pass a pattern and a fact and say if they match. An example would be

(heroes (hitpoints=hp) (mana=m)) 

should match

(Morphling (hitpoints 435) (mana 260))

it should also be able to also do numeric comparisons of if a number is greater or lesser. Like if another heroes mana is less that Morphling.

Upvotes: 12

Views: 15493

Answers (3)

Rainer Joswig
Rainer Joswig

Reputation: 139261

Simple pattern matching functionality is explained in various Lisp books.

and others.

Above books explain implementing pattern matching in Lisp very well.

Libraries exist, for example trivia, cl-match, and various others.

Upvotes: 17

m7d
m7d

Reputation: 736

I don't want to short circuit any learning you need to do for school (if that is the context in which this project is necessitated), but you could study the cl-ppcre library, http://weitz.de/cl-ppcre/, to see how an experienced Lisper does it. You could download the source and study it to understand. I would also second the book by Norvig, http://norvig.com/paip.html, mentioned above. You can learn so much from that book.

Upvotes: 3

Chris Perkins
Chris Perkins

Reputation: 809

I think you may want the CL-Unification library: http://common-lisp.net/project/cl-unification/

Upvotes: 4

Related Questions