“应用” 和 “应用” 和有什么不一样?和“mapcar”在 Lisp 中

发布于 2024-11-05 12:28:06 字数 136 浏览 0 评论 0原文

(defun describe-paths (location edges) 
(apply #'append (mapcar #'describe-path (cdr (assoc location edges)))))
(defun describe-paths (location edges) 
(apply #'append (mapcar #'describe-path (cdr (assoc location edges)))))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李不 2024-11-12 12:28:06

有哪些相似之处?或者,这里还潜藏着另一个问题吗?

是我所知道的。引用只是摘录,链接包含可能与特定“Lisp”相关或不相关的示例。)

(来自 elisp 的链接,因为这 org/software/emacs/emacs-lisp-intro/html_node/mapcar.html">mapcar

<块引用>

mapcar 是一个函数,它依次调用其第一个参数及其第二个参数的每个元素。第二个参数必须是一个序列。

应用(在调用函数中)

<块引用>

apply 使用参数调用函数,就像 funcall 一样,但有一个区别:最后一个参数是一个对象列表,它们作为单独的参数传递给函数,而不是单个列表。我们说 apply 扩展这个列表,以便每个单独的元素都成为一个参数。

快乐编码。

What are the similarities? Or, is there another question lurking here?

(Links from elisp, because that is what I know. The quotes are just excerpts and the links contain examples which may or may not be relevant in a particular "Lisp".)

mapcar

mapcar is a function that calls its first argument with each element of its second argument, in turn. The second argument must be a sequence.

apply (in Calling Functions)

apply calls function with arguments, just like funcall but with one difference: the last of arguments is a list of objects, which are passed to function as separate arguments, rather than a single list. We say that apply spreads this list so that each individual element becomes an argument.

Happy coding.

阿楠 2024-11-12 12:28:06

describe-paths 函数(来自 Land of Lisp 中基于文本的冒险游戏!) 生成从给定位置出发的路径的描述。 Land of Lisp 中的第 74-77 页解释了示例中 mapcarappend 的作用。

(cdr (assoc location Edges)) 提供从该位置出发的所有路径的列表,例如 living-room 位置的路径:

((GARDEN WEST DOOR)
 (ATTIC UPSTAIRS LADDER))

mapcar 调用该函数每个的 describe-path路径,将路径描述收集到一个列表中,其中每个子列表都是一个路径描述:

((THERE IS A DOOR GOING WEST FROM HERE.)
 (THERE IS A LADDER GOING UPSTAIRS FROM HERE.)) 

接下来,append 函数应用于路径描述列表,将其连接成一个平面列表:

(THERE IS A DOOR GOING WEST FROM HERE. THERE IS A 
  LADDER GOING UPSTAIRS FROM HERE.)

The describe-paths function (from the text based adventure game in Land of Lisp!) generates descriptions for the paths going from a given location. Page 74-77 in Land of Lisp explains the roles of mapcar and append in the example.

The (cdr (assoc location edges)) provides a list of all paths going from the location, such as these for the living-room location:

((GARDEN WEST DOOR)
 (ATTIC UPSTAIRS LADDER))

The mapcar calls the function describe-path for each of the paths, collecting the path descriptions into a list where each of the sublists is a path description:

((THERE IS A DOOR GOING WEST FROM HERE.)
 (THERE IS A LADDER GOING UPSTAIRS FROM HERE.)) 

Next the append function is applied to the list of path descriptions, concatenating it into a flat list:

(THERE IS A DOOR GOING WEST FROM HERE. THERE IS A 
  LADDER GOING UPSTAIRS FROM HERE.)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文