将函数映射到elisp中的两个列表 [英] Mapping a function over two lists in elisp

查看:86
本文介绍了将函数映射到elisp中的两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在常见的Lisp中,我可以这样做:

In common lisp I can do this:

(mapcar #'cons '(1 2 3) '(a b c))

=> ((1 . A) (2 . B) (3 . C))

我如何在elisp中做同样的事情?当我尝试时,出现错误:

How do I do the same thing in elisp? When I try, I get an error:

(wrong-number-of-arguments mapcar 3)

如果elisp的mapcar一次只能处理一个列表,那么将两个列表组合成一个列表的理想方式是什么?

If elisp's mapcar can only work on one list at a time, what is the idomatic way to combine two lists into an alist?

推荐答案

您想要mapcar*,它接受一个或多个序列(不只是Common Lisp中的列表),而且对于一个序列参数,其作用与常规mapcar.

You want mapcar*, which accepts one or more sequences (not just lists as in Common Lisp), and for one sequence argument works just like the regular mapcar.

(mapcar* #'cons '(1 2 3) '(a b c))
((1 . A) (2 . B) (3 . C))

即使未定义,您也可以轻松地自己滚动:

And even if it weren’t defined, you could easily roll your own:

(defun mapcar* (f &rest xs)
  "MAPCAR for multiple sequences"
  (if (not (memq nil xs))
    (cons (apply f (mapcar 'car xs))
      (apply 'mapcar* f (mapcar 'cdr xs)))))

这篇关于将函数映射到elisp中的两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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