Re: pattern-matching in LISP?



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/
.



Relevant Pages

  • Re: Formal and informal type systems?
    ... more potent as it supports type inference and can be formally reasoned ... That has nothing to do with the type system. ... Common Lisp has argument pattern matching ...
    (comp.lang.ada)
  • Re: Does ANSI Common Lisp have pattern matching?
    ... It's interesting that Harrop confuses "pattern matching can be used ... Perl compatible regular expressions are not part of common lisp either. ... if I needed pattern matching I'd just load one of the existing pattern matching libraries and use it. ... Note also that these provide a *variety* of solutions using different algorithms since, as even Harrop admits, "pattern matching" is not one well defined, stable, cut and dried thing. ...
    (comp.lang.lisp)
  • Re: Flamebait if I ever saw it
    ... many more people use common lisp than all the functional languages *combined*. ... Common Lispers don't use pattern matching not because the Common Lisp libraries for pattern matching aren't like ml style pattern matching - who would possibly care? ... - practically no one uses *ml anyway - it's not as if these languages are some sort of interoperable library standard like C/C++. ...
    (comp.lang.lisp)
  • Re: Does ANSI Common Lisp have pattern matching?
    ... a very good reason to provide no pattern matching whatsoever. ... but to leave implementors and users room to experiment. ... This is exactly the situation with garbage collection in Common Lisp: ... memory management is not addressed by the Common Lisp standard, ...
    (comp.lang.lisp)
  • Re: Writing shared libraries using Lisp
    ... > Is it possible to write shared libraries in common lisp? ... ECL in this regard, ...
    (comp.lang.lisp)