Search code examples
listbindingcommon-lisplet

Getting value in a let-binded list (Common Lisp)


In below situation, I want to get same results for a and (car b) like (10 10). But just symbol A is shown as a result of (car b). Can we get 10 with like (something (car b)) code?

(let* ((a 10)
       (b '(a)))
  (list a (car b))) ; (10 A)
  ;; Hoping to get (10 10) with (list a (something (car b)))

I also tried below ones but couldn't get the answer.

(let* ((a 10)
       (b '(a))
       (c `(,a)))
  (list a ; 10
        'a ; A
        (car b) ; A
        (eq (car b) 'a) ; T
        (car c) ; 10
        ;; (eval 'a) ; Error - The variable A is unbound.
        ;; (symbol-value (car b)) ; Error - The variable A is unbound.
        ))

My motivation is to make symbols and params lists with let like below. But the part of foo is not working as I expected now. I believe above question will solve (or help) this also. (This part is not question)

(defmacro my-let (binds &body body)
  `(let ((symbols (mapcar #'car ',binds))
         (params (mapcar #'(lambda (x) (eval (cadr x))) ',binds))) ; I may need to modify here
     (let ,binds
       ,@body)))

(defun foo (bar)
  (my-let ((b bar))
    (list symbols params)))

(foo "baz") ; Error - The variable BAR is unbound.

Solution

  • Would something like this solve your problem?

    (defmacro my-let ((&rest bindings) &body body)
      `(let ,bindings
         (let ((symbols ',(mapcar #'car bindings))
               (values (list ,@(mapcar #'car bindings))))
           ,@body)))
                                                                                                                                                                                                                                                  
    ((lambda (bar)
       (my-let ((b bar))
         (list symbols values)))
     "baz")
    
    ((B) ("baz"))