Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 在Erlang中创建列表列表_List_Erlang - Fatal编程技术网

List 在Erlang中创建列表列表

List 在Erlang中创建列表列表,list,erlang,List,Erlang,例如,我有一个列表[1,3,5]和另一个列表[2,4,6],我如何将这两个列表附加到这样的列表中:[[1,3,5],[2,4,6] 如果我在末尾添加另一个列表,使其看起来像[[1,3,5],[2,4,6],[7,8,9],,如何操作该列表?您只需要创建一个包含这两个列表的列表 L1 = [1,3,5], L2 = [2,4,6], [L1,L2]. A = [1,3,5], B = [2,4,6], [A, B]. 在shell中,您可以: > Herp = lol:append([

例如,我有一个列表
[1,3,5]
和另一个列表
[2,4,6]
,我如何将这两个列表附加到这样的列表中:
[[1,3,5],[2,4,6]


如果我在末尾添加另一个列表,使其看起来像
[[1,3,5],[2,4,6],[7,8,9],
,如何操作该列表?

您只需要创建一个包含这两个列表的列表

L1 = [1,3,5], 
L2 = [2,4,6],
[L1,L2].
A = [1,3,5],
B = [2,4,6],
[A, B].
在shell中,您可以:

> Herp = lol:append([1,3,4], lol:new()).
[[1,2,3]]
> Derp = lol:append([4,5,6], Herp).
[[4,5,6],[1,2,3]] 
> lol:head(Derp).
[4,5,6]

我把剩下的留给用户做练习。

旁注:在erlang中,如果列表中有
L=[A,B]
,并且想要添加
C
,我们通常会在前面添加
C
[C | L]
,这与
[C,A,B]
相同。随着列表越来越大,在列表末尾插入非常昂贵。
-module(lol).
-export([new/0, append/2, head/1, tail/1]).

new() -> [].

append(H, []) when is_list(H) -> [H];
append(H, T) when is_list(H) -> [H | T].

head([H | _]) when is_list(H) -> H.
tail([_ | T]) -> T.
> Herp = lol:append([1,3,4], lol:new()).
[[1,2,3]]
> Derp = lol:append([4,5,6], Herp).
[[4,5,6],[1,2,3]] 
> lol:head(Derp).
[4,5,6]