Re: legitimate closure?



Mirko.Vukovic@xxxxxxxxx writes:

This closure works on clisp on cygwin+windows:
cl-user> (let ((list (list 1 2 3 4 5))
(item 4))
(delete item list :test #'(lambda (unused b) (= item b))))
(1 2 3 5)

I used closure for lambda to get "item" from delete's first argument

Why? DELETE already pass it to your TEST function, in the UNUSED
parameter, since you gave ITEM to DELETE.


The straightforward version is
cl-user> (let ((list (list 1 2 3 4 5))
(item 4))
(delete item list :test #'(lambda (a b) (= a b))))

I'm not sure why would this ever be useful, but I was surprised that
lambda captured it.

It did not. You passed explicitely ITEM to DELETE, and DELETE passes
it explicitely to your TEST function.


C/USER[6]> (let* ((list (list (cons 1 1) (cons 2 2) (cons 3 3) (cons 4 4) (cons 5 5)))
(item (fourth list)))
(delete item list :test (lambda (a b)
(print `(is a identical to item? --> ,(eq a item)))
(print (member b list))
(eq a b))))

(IS A IDENTICAL TO ITEM? --> T)
((1 . 1) (2 . 2) (3 . 3) (4 . 4) (5 . 5))
(IS A IDENTICAL TO ITEM? --> T)
((2 . 2) (3 . 3) (4 . 4) (5 . 5))
(IS A IDENTICAL TO ITEM? --> T)
((3 . 3) (4 . 4) (5 . 5))
(IS A IDENTICAL TO ITEM? --> T)
((4 . 4) (5 . 5))
(IS A IDENTICAL TO ITEM? --> T)
((5 . 5))
((1 . 1) (2 . 2) (3 . 3) (5 . 5))



--
__Pascal Bourguignon__ http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
.



Relevant Pages

  • Re: closures and dynamic binding
    ... The double lambda is conceptually more sound in some ... I agree that the default argument syntax is an abuse, ... don't think there's a way to create a closure in Python without ... overrides the outer scope. ...
    (comp.lang.python)
  • Re: Lisp in hardware
    ... > pre-computed index into an environment. ... You have to "pre-compute" a LAMBDA just as much (or rather, ... because the closure object has to hold the captured lexical ...
    (comp.lang.lisp)
  • Re: Cost of closures
    ... a closure consists of a piece of code and a set of lexical ... by the same lambda while the environment part is different. ... in a register or on the stack. ... it must indirect through the `this' pointer. ...
    (comp.lang.lisp)
  • Re: I thought this was the one that worked?
    ... It's still a closure. ... closure for a block regardless. ... That lambda still has a reference to ...
    (comp.lang.ruby)
  • Re: Anonymous methods, blocks etc. (Cont. default block params)
    ... What does "closure capabilities" mean ... >> so far suggested for anonymous functions. ... > for def style semantics (i.e. 'true' anonymous function, opaque scope, ... in a lambda, it will return from the lambda only. ...
    (comp.lang.ruby)