Re: from source-code CLISP-2.38
- From: "micromoog" <micromoog@xxxxxxxxx>
- Date: 29 Jun 2006 11:20:29 -0700
arnuld wrote:
hai folks
i was browsing through the source-code of CLISP 2.38, in the file named
"LOOP.lisp" i came across the following function:
(defun destructure-vars (pattern)
(let ((vars '()))
(labels ((accumulate (pattern)
(cond ((null pattern))
((atom pattern) (push pattern vars))
(t
(accumulate (car pattern))
(accumulate (cdr pattern))))))
(accumulate pattern))
(nreverse vars)))
;; OUTPUT
(destructure-vars '(1 2 3 4)) ==> '(1 2 3 4)
(destructure-vars '(1 (((2))) (((3)) 4))) ==> '(1 2 3 4)
ok i got it this function destructures the elements of a tree into a
single list, BUT, then i took out "accumulate" from the "labels" and
tried it as a stand-alone function.
(defun accumulate (pattern)
(let ((vars '()))
(cond ((null pattern))
((atom pattern) (push pattern vars))
(t (accumulate (car pattern))
(accumulate (cdr pattern))))
(nreverse vars)))
;; OUTPUT
(accumulate '(1 2 3 4)) ==> NIL
(accumulate '(1 (((2))) (((3)) 4))) ==> NIL
why it does not produce '(1 2 3 4) even though it's same (only "labels"
is not here)
why two same functions are producing different outputs?
how can i make 2nd one to produce same output as 1st one. i have Debian
"Sarge" running with CLISP - 2.33.2. (i also have SBCL 0.9.11)
thanks
-- arnuld
In the first case, when accumulate recursively calls itself, it's still
all within the context of the same let. In the 2nd case, the let
occurs within accumulate, so with each recursive call you're defining a
new instance of vars.
This is why the CLISP code is written that way; the outer function
destructive-vars is called once, so the let environment is only created
once, but the inner function accumulate is called many times inside the
same environment.
.
- Follow-Ups:
- Re: from source-code CLISP-2.38
- From: arnuld
- Re: from source-code CLISP-2.38
- References:
- from source-code CLISP-2.38
- From: arnuld
- from source-code CLISP-2.38
- Prev by Date: Re: What would a modern LispOS look like?
- Next by Date: KnowOS (Was: What would a modern LispOS look like?)
- Previous by thread: from source-code CLISP-2.38
- Next by thread: Re: from source-code CLISP-2.38
- Index(es):
Relevant Pages
|
|