通过嵌套列表LISP递归 [英] Recursing Through Nested List LISP

查看:73
本文介绍了通过嵌套列表LISP递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何遍历嵌套列表?

例如,给定:'((A 1 2) (B 3 4))

如何在每个嵌套子列表的第二个元素中加2?

How would I add 2 to the second element in each nested sublist?

(defun get-p0 (points)
    (loop for x from 0 to
            (-  (list-length    points) 1) do
                (+ 2 (cadr (nth x points)))
    )
)

我不太确定为什么(get-p0 '((A 1 2) (B 3 4)))返回NIL.

I'm not really sure why (get-p0 '((A 1 2) (B 3 4))) returns NIL.

推荐答案

我会选择这样的东西:

(loop for (letter x y) in '((A 1 2) (B 3 4))
     collect (list letter (+ 2 x) y))

原因:它更短,并且您无需测量列表的长度即可对其进行迭代(为什么要这么做?)

The reason: it's shorter and you don't measure the length of the list in order to iterate over it (why would you do that?)

这篇关于通过嵌套列表LISP递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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