Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 688
gizgok Avatar asked Sep 26 '10 16:09

gizgok


People also ask

Does lisp have pattern matching?

The ANSI Common Lisp standard does not include facilities for pattern matching, but libraries existed for this task and Trivia became a community standard. For an introduction to the concepts of pattern matching, see Trivia's wiki.

What is pattern matching in Erlang?

Pattern matching is used for assigning values to variables and for controlling the flow of a program. Erlang is a single assignment language, which means that once a variable has been assigned a value, the value can never be changed. Pattern matching is used to match patterns with terms.

What is pattern matching explain with example?

Pattern matching is the process of checking whether a specific sequence of characters/tokens/data exists among the given data. Regular programming languages make use of regular expressions (regex) for pattern matching.

How pattern matching is used in scripting language?

JavaScript uses the RegExp (short for Regular Expression) object to handle pattern matching. This object holds the pattern definition, as well as provides methods for performing matching. You'll begin by learning how to define patterns and then by learning how to use the RegExp objects to test for pattern matches.


2 Answers

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.

like image 158
m7d Avatar answered Dec 04 '22 00:12

m7d


Simple pattern matching functionality is explained in various Lisp books.

  • Lisp, 3rd edition, Winston/Horn

  • Paradigms of Artificial Intelligence Programming, Case Studies in Common Lisp, Peter Norvig

and others.

Above books explain implementing pattern matching in Lisp very well.

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

like image 23
Rainer Joswig Avatar answered Dec 04 '22 00:12

Rainer Joswig