替换为Lisp [英] Substitutions in Common Lisp

查看:69
本文介绍了替换为Lisp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个带有两个这种类型的参数的函数:

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

substitutions (list_one, list_two)

list_one始终具有这种形式(字母可以根据输入内容进行更改):

list_one has always this form (letters can change according to the input):

(1 ((1 2 ((1 2 r) (3 2 t) (4 3 c))) (3 4 ((5 6 y) (5 7 i)))))

list_two始终具有这种形式(数字可以根据输入而变化):

list_two has always this form (numbers can change according to the input):

(2 3 4 5 6)

我想用这种方式代替:

r-> 2
t -> 3
c -> 4
y -> 5
i -> 6

你能帮我吗?

推荐答案

如果列表正确,则可以使用loop宏对其进行迭代,并弹出可访问的自由变量中的参数:

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)

这篇关于替换为Lisp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆