简体   繁体   中英

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.

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. map applies function1 to every element of such a list, and returns a list of results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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