Re: pattern-matching in LISP?
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Sun, 29 Apr 2007 03:08:49 +0200
Chris Schumacher wrote:
I noticed that in the functional language ML there is a way to break a function's input into various forms in the function header.
For instance:
split(a::b::c) (where :: is ML's equivalent of cons)
This also allows an interesting kind of function overloading where you can contain several function definitions in a single declaration, each keyed by the format of the input.
I attempted to do something similar in LISP, using:
defun ( split (cons A B) )
But it didn't appear to work.
Your code isn't even close to the s-expression syntax of Lisp, so it's no wonder that it doesn't work. That's not related to pattern matching.
There are pattern matching libraries for Lisp. (That's the nice thing of a programmable programming language - you can just extend it without the need to mess with the internals of an implementation.)
Neither Common Lisp nor Scheme, the two major Lisp dialects, have anything built in in that regard.
Common Lisp has a low-level destructuring-bind form, but that's used when you already know what the structure of your data is. There have been suggestions for building a destructuring-case on top of it, but that's probably not the best way to do it.
In Common Lisp, you would probably try to arrange your code differently and take advantage of generic functions. That also gives you the advantage of better potential to dynamically evolve your code without sacrificing efficiency.
Some Scheme implementations come with pattern matching provided as built-in libraries. For example, Bigloo seems to have a nice case-match / case-lambda facility.
It all depends on what you actually want to do (and unfortunately, you don't give enough details in that regard).
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
.
- References:
- pattern-matching in LISP?
- From: Chris Schumacher
- pattern-matching in LISP?
- Prev by Date: Re: programmatically determine if argument is list compatible to a given lambda list
- Next by Date: Re: Any macro for inserting math "normally"
- Previous by thread: pattern-matching in LISP?
- Next by thread: Re: pattern-matching in LISP?
- Index(es):
Relevant Pages
|