Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!samsung!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.scheme Subject: Re: Destructuring / pattern-matching (was: Multiple return values) Message-ID: <5261@goanna.cs.rmit.oz.au> Date: 15 Apr 91 10:37:38 GMT References: <12542.9104111347@subnode.aiai.ed.ac.uk> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 52 In article <12542.9104111347@subnode.aiai.ed.ac.uk>, jeff@aiai.edinburgh.ac.UK (Jeff Dalton) writes: > Another problem with patterns in general is that they usually do not > respect the abstract structure of objects. If something is the third > element of a list, a pattern will depend on it being the third > element; if something is a slot of a struct, the pattern will depend > on it being a slot (rather than being accessed some other way). This > is a serious hassle in Prolog, where changing the structure of a term > often requires changing patterns all over the program; and since the > contents of terms are accessed positionally, it's easy to make a > mistake. > When people talk of the advantages of pattern-matching in Prolog and > functional languages, they seldom mention that it has this disadvantage > too. (Self-advertisement: "The Craft of Prolog" _does_ talk about this and what to do about it.) A possible answer in Lisp-like languages would be to introduce a new form (define-pattern ( ...) ) where are sufficiently restricted for a simple program to figure out how to invert them (say: each name must appear exactly once in the pattern, ,@ may appear only at the end of a list, something like that). One also introduces a new , variant: ,=( ...). If I used (define-pattern (assoc-link Key Val Rest) `((,Key ,@Val) ,@Rest)) then (let ((foo `,=(assoc-link foo 9 ,bar))) ...) would have the effect of (let ((foo `((foo . 9) ,@bar))) ...) and (destructure `,=(assoc-link ,k ,v ,bar) foo ...) would have the effect of (destructure `((,k ,@v) ,bar) foo ...) It's certainly doable. -- It is indeed manifest that dead men are formed from living ones; but it does not follow from that, that living men are formed from dead ones. -- Tertullian, on reincarnation.