Re: from source-code CLISP-2.38




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.

.



Relevant Pages