我在文档中找到的所有相关内容都是 ++ 和 concat。
我一开始认为执行以下操作会给我带来我想要的:
[1, 3, 4] ++ [4, 5, 6]
但如你所知,只给出 [1, 2, 3, 4, 5, 6]。
我需要做什么才能接收 [1, 2, 3] 和 [4, 5, 6] 并输出 [[1, 2, 3], [4, 5, 6]]?
正如评论中提到的,获取两个列表并将它们组合成一个新列表的函数可以定义为:
combine :: [a] -> [a] -> [[a]]
combine xs ys = [xs,ys]
不能多次应用此函数来创建任意数量的列表的列表。这样的函数将采用单个列表和列表列表,并将单个列表添加到列表列表中,因此它的类型为:
push :: [a] -> [[a]] -> [[a]]
这只是(:)
, 尽管:
push = (:)
As also mentioned in the comments, the value [x,y]
can also be written as x : y : []
.1 Since both cases can be done with (:)
, I would guess that what you really want to use is (:)
, sometimes consing onto []
and sometimes onto a non-empty list.
1 In fact, [x,y]
is just syntactic sugar http://en.wikipedia.org/wiki/Syntactic_sugar for x:y:[]
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)