Substitutions in Common Lisp

前端 未结 2 499
谎友^
谎友^ 2021-01-29 10:13

I’m trying to write a function with two arguments of this type:

substitutions (list_one, list_two)

list_one has always this form (letters can c

2条回答
  •  庸人自扰
    2021-01-29 10:55

    If the lists are proper you can iterate them with the loop macro and pop off the arguments in the accessible free variable:

    (defun template-replace (template replacements)
      (labels ((iterate (template)
                 (loop :for element :in template
                       :collect
                       (cond ((consp element) (iterate element))
                             ((symbolp element) (pop replacements))
                             (t element)))))
        (iterate template)))
    
    
    (template-replace '(1 rep (4 rep (9 rep)) rep) '(foot inch mm multiplied))
    ; ==> (1 foot (4 inch (9 mm)) multiplied)
    

提交回复
热议问题