On Feb 13, 5:38 pm, Francogrex <······@grex.org> wrote:
> Assoc:
> Args: (ITEM ALIST &KEY (TEST '#'EQL) TEST-NOT (KEY '#'IDENTITY))
> Returns the first pair in ALIST whose car is equal (in the sense of
> TEST) to
> ITEM. Returns NIL if no such pair exists...
>
> Suppose we have this simple example:
> : (setf my-list '((test 1) (paper 2) (pen 2) (paper 3) (paper 4) (pen
> 7)))
> : (assoc 'paper my-list)
> : (PAPER 2)
>
> OK but is there any way/function, maybe a variation of assoc, to get a
> list of all pairs in my-list with 'paper in them (instead of just the
> 1st ocurrence) without resorting to loops? Thanks
(remove 'paper '((test 1) (paper 2) (pen 2) (paper 3) (paper 4) (pen
7))
:test (complement 'eql)
:key 'first)
Cheers
--
Marco
On Feb 13, 6:31 pm, Marco Antoniotti <·······@gmail.com> wrote:
> On Feb 13, 5:38 pm, Francogrex <······@grex.org> wrote:
>
> > Assoc:
> > Args: (ITEM ALIST &KEY (TEST '#'EQL) TEST-NOT (KEY '#'IDENTITY))
> > Returns the first pair in ALIST whose car is equal (in the sense of
> > TEST) to
> > ITEM. Returns NIL if no such pair exists...
>
> > Suppose we have this simple example:
> > : (setf my-list '((test 1) (paper 2) (pen 2) (paper 3) (paper 4) (pen
> > 7)))
> > : (assoc 'paper my-list)
> > : (PAPER 2)
>
> > OK but is there any way/function, maybe a variation of assoc, to get a
> > list of all pairs in my-list with 'paper in them (instead of just the
> > 1st ocurrence) without resorting to loops? Thanks
>
> (remove 'paper '((test 1) (paper 2) (pen 2) (paper 3) (paper 4) (pen
> 7))
> :test (complement 'eql)
> :key 'first)
>
Very nice.
bobi
> Cheers
> --
> Marco
Slobodan Blazeski <·················@gmail.com> wrote:
+---------------
| Marco Antoniotti <·······@gmail.com> wrote:
| > (remove 'paper '((test 1) (paper 2) (pen 2) (paper 3) (paper 4) (pen 7))
| > :test (complement 'eql)
| > :key 'first)
|
| Very nice.
+---------------
One tiny quibble: (COMPLEMENT 'EQL) won't work [at least, not portably]
since COMPLEMENT takes a function, not a function designator. You need
to write it as (COMPLEMENT #'EQL). On the other hand, :TEST-NOT [1]
does take a function designator and thus this works portably:
> (remove 'paper '((test 1) (paper 2) (pen 2) (paper 3) (paper 4) (pen 7))
:test-not 'eql :key 'first)
((PAPER 2) (PAPER 3) (PAPER 4))
>
-Rob
[1] Which I stubbornly persist in using despite its "deprecated" status! ;-}
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
On Feb 16, 4:22 am, ····@rpw3.org (Rob Warnock) wrote:
> Slobodan Blazeski <·················@gmail.com> wrote:
> +---------------
> | Marco Antoniotti <·······@gmail.com> wrote:
> | > (remove 'paper '((test 1) (paper 2) (pen 2) (paper 3) (paper 4) (pen 7))
> | > :test (complement 'eql)
> | > :key 'first)
> |
> | Very nice.
> +---------------
>
> One tiny quibble: (COMPLEMENT 'EQL) won't work [at least, not portably]
> since COMPLEMENT takes a function, not a function designator. You need
> to write it as (COMPLEMENT #'EQL). On the other hand, :TEST-NOT [1]
> does take a function designator and thus this works portably:
>
> > (remove 'paper '((test 1) (paper 2) (pen 2) (paper 3) (paper 4) (pen 7))
> :test-not 'eql :key 'first)
>
> ((PAPER 2) (PAPER 3) (PAPER 4))
> >
>
> -Rob
>
> [1] Which I stubbornly persist in using despite its "deprecated" status! ;-}
Good point! (LW is forgiving on COMPLEMENT.) But you see why I used
COMPLEMENT and not :TEST-NOT.
Cheers
--
Marco
>
> -----
> Rob Warnock <····@rpw3.org>
> 627 26th Avenue <URL:http://rpw3.org/>
> San Mateo, CA 94403 (650)572-2607