是否“映射"?一定会产生额外的嵌套级别吗? [英] Does "map" necessarily produce an additional level of nesting?

查看:48
本文介绍了是否“映射"?一定会产生额外的嵌套级别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用嵌套的 map 是否会自动创建另一层嵌套?这是我使用的一个基本示例:

Does using nested maps automatically create another level of nesting? Here is a basic example that I used:

; One level
(map (lambda (x1)
   "Hi")
 '(1))

; Two levels
(map (lambda (x1)
   (map (lambda (x2)
          "Hi")
        '(1)))
     '(1))

; Three levels
(map (lambda (x1)
   (map (lambda (x2)
      (map (lambda (x3)
          "Hi")
           '(1)))
        '(1)))
     '(1))

; Four levels
(map (lambda (x1)
   (map (lambda (x2)
      (map (lambda (x3)
         (map (lambda (x4)
          "Hi")
           '(1)))
        '(1)))
     '(1)))
   '(1))

("Hi")
(("Hi"))
((("Hi")))
(((("Hi"))))

或者,是否有任何反例添加另一个 map 不会产生额外的嵌套?我无法了解"map 如何添加另一个级别.例如,对于第一级:

Or, are there any counterexamples where adding another map will not produce additional nesting? I'm having trouble 'getting' how map adds another level. For example, for the first level:

(map (lambda (x1)
   "Hi")
 '(1))

我知道列表中有一个元素,并且对于列表中的每个元素"——我们将返回列表中那个位置的元素 "Hi",因此对于第一级我们得到(Hi").

I understand that there is one element in the list, and 'for each element' in the list -- we will return the element "Hi"at that position in the list, so for the first level we get ("Hi").

但是,例如,如何从列表 (Hi") 中获得第二级嵌套列表?我知道我问了很多与 map 和嵌套相关的问题,但希望我能理解从 1 级别到 2 的过程> 我可以弄清楚其余的...

But then how, for example, from the list ("Hi") do we get a nested list at the second level? I know I've asked a lot of questions related to map and nesting, but hopefully if I can just understand the going from 1 level to 2 I can figure out the rest...

推荐答案

map 收集所有函数调用的返回值,并将它们包装在另一个列表中.

map collects the return values of all the function calls, and wraps them in another list.

如果函数返回一个列表,您将获得一个列表列表.

If the function returns a list, you'll get a list of lists.

所以如果你有嵌套的地图,你总是得到嵌套列表作为最终结果.并且每一级映射都增加了另一级嵌套.

So if you have nested maps, you always get nested lists as the final result. And each level of mapping adds another level of nesting.

请注意,只有当您在每个级别实际返回 map 的值时,这才是正确的.你可以用它做其他事情,例如

Note that this is only true if you're actually returning the value of map at each level. You could do other things with it, e.g.

;; Two levels
(map (lambda (x1)
       (length (map (lambda (x2)
                      "Hi")
                    '(1))))
     '(1))

这将返回(1)

这篇关于是否“映射"?一定会产生额外的嵌套级别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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