简体   繁体   English

Lisp遍历函数中的列表

[英]Lisp looping through a list in a function

I currently have a method and I keep calling it by doing the following 我目前有一种方法,并且通过执行以下操作不断调用它

(function1 (first lst))

(function1 (second lst))

(function1 (third lst))

This goes on to five. 这继续到五个。 I'm wondering if there is a function I can create which will do this for me, so when created I call one method instead of calling it five times. 我想知道是否可以创建一个函数来帮我实现这一功能,因此在创建时我调用一个方法而不是调用五次。

Given your previous question I would recommend looking into mapcar and reduce and try getting a feel for them. 鉴于您之前的问题,我建议您研究一下mapcar减少并尝试使他们感到满意。

They take a bit of getting used to if you are more used to loops but they are often a better solution. 如果您更习惯循环,它们会有些适应,但是它们通常是更好的解决方案。

For example: 例如:

(mapcar #'function lst)

Will call function on each element of the list returning a list of the results. 将在列表的每个元素上调用函数,并返回结果列表。

It almost seems like you don't care of the result. 似乎您根本不​​在乎结果。 Then you can do 那你可以做

(mapc #'function lst)
(map function1 (take lst 5))

take takes the first elements of lst and forms a list out of them. take获取lst的第一个元素,并take形成一个列表。 map applies function1 to every element of such a list, and returns a list of results. mapfunction1应用于此类列表的每个元素,并返回结果列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM