Emacs Lisp - does mapcar not apply a function to all elements?

I have a function that takes a list and replaces some elements. I built it as a closure, so the free variable cannot be changed outside the function.

(defun transform (elems)
  (lexical-let ( (elems elems) )
    (lambda (seq)
      (let (e)
    (while (setq e (car elems))
      (setf (nth e seq) e)
      (setq elems (cdr elems)))
    seq))))

I call this in the list of lists.

(defun tester (seq-list)
  (let ( (elems '(1 3 5)) )
    (mapcar (transform elems) seq-list)))

(tester (list (reverse (number-sequence 1 10)) 
          '("a" "b" "c" "d" "e" "f")))
=> ((10 1 8 3 6 5 4 3 2 1) ("a" "b" "c" "d" "e" "f"))

This function does not seem to apply to the second list item provided to the tester (). However, if I explicitly apply this function to individual elements, it works ...

(defun tester (seq-list)
  (let ( (elems '(1 3 5)) )
    (list (funcall (transform elems) (car seq-list))
          (funcall (transform elems) (cadr seq-list)))))

(tester (list (reverse (number-sequence 1 10)) 
          '("a" "b" "c" "d" "e" "f")))
=> ((10 1 8 3 6 5 4 3 2 1) ("a" 1 "c" 3 "e" 5))

If I write a simple function using the same concepts as above, then the wireframe seems to work ... What can I do wrong?

(defun transform (x)
  (lexical-let ( (x x) )
    (lambda (y) 
      (+ x y))))

(defun tester (seq)
  (let ( (x 1) )
    (mapcar (transform x) seq)))

(tester (list 1 3))    
=> (2 4)

thank

+3
source share
1 answer

elems ; nil. , transform .

:

(defun transform (elems)
  (lexical-let ((elems elems))
    (lambda (seq)
      (dolist (e elems seq)
        (setf (nth e seq) e)))))
+2

Source: https://habr.com/ru/post/1748637/


All Articles