Erlang列表连接,奇怪的“|”标志 [英] Erlang List concatenation, weird "|" sign

查看:185
本文介绍了Erlang列表连接,奇怪的“|”标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了与Erlang成为熟人,我正在尝试
来编写自己的Bubblesort算法。现在,我有
我的模块中的以下代码:

  -module(mysort)。 
-export([bubblesort / 1])。

bubblesort(L) - >
sort_sequence(L,[])。

sort_sequence([H1 | [H2 | T]],排序) - >
如果H2> = H1 - >
sort_sequence(T,Sorted ++ [H1,H2]);
H2 < H1 - >
sort_sequence(T,Sorted ++ [H2,H1])$ ​​b $ b end;
sort_sequence([H | T],排序) - >
排序++ H;
sort_sequence([],Sorted) - >
排序。

首先:请不要给我建议我的代码
i想自己弄清楚^^



问题是:如果我说 mysort:bubblesort([2,1,3 ,4,5])。
输出正如我所料: [1,2,3,4,5]



但如果我说 mysort:bubblesort([2,1,3,5,4])。
的输出是: [1,2,3,5 | 4]。



我唯一的问题是:这个|签名意味着列表之间?!



谢谢大家!

解决方案

列表可以有两种形式:它们是空的( [] ),或者它有一个头和尾( [H | T] )。所以 [] 是一个空列表, [1 | []] 是一个列表,其头为1,尾部为 [] [1 | 2] / code>是一个列表,其头部为1,尾部为2。



列表被称为正确的列表,如果是空的或它的尾巴是一个适当的列表。所以 [] 是一个正确的列表,因为它的空和 [1 | []] 是一个正确的列表,因为它的尾是空的列表(这是一个正确的列表),但是 [1 | 2] 不是一个正确的列表,因为它的尾部是2和2不是一个正确的列表。 / p>

由于正确的列表是最常见的列表类型,因此读取和写入它们作为这样的嵌套列表很麻烦,它们有一个特殊的语法:正确的列表是可以用逗号分隔子列表的头部来写出。所以如果我们有正确的列表 [1 | [2 | [3 | []]]] 它显示为 [1,2,3] (我们也可以这样写),这是很多更可读。



此格式也用于显示不正确列表的开始,如果它们正确到正确的尾部,那么将分开一个 | 。所以例如,如果我们有不正确的列表 [1 | [2 | [3 | 4]]] (这是不正确的,因为最内层的尾巴是4,这不是一个正确的列表),它将显示为 [1,2,3 | 4] 将其与正确的列表 [1 | [2 | [3 | [4 | []]]]] ,将显示为 [1,2,3,4]



所以如果你看到这样的东西,你以某种方式创建一个列表,其尾部不是一个正确的列表。


in order to become familar with Erlang, i'm trying to write my own Bubblesort Algorithm. Right now, i have the following code in my module:

-module(mysort).
-export([bubblesort/1]).

bubblesort(L) ->
    sort_sequence(L, []).

sort_sequence([H1|[H2|T]], Sorted) ->
    if H2 >= H1 ->
        sort_sequence(T, Sorted ++ [H1, H2]);
    H2 < H1 ->
        sort_sequence(T, Sorted ++ [H2, H1])
    end;
sort_sequence([H|T], Sorted) ->
    Sorted ++ H;
sort_sequence([], Sorted) ->
    Sorted.

first of all: please don't give me suggestions to my code i want to figure it out myself ^^

the problem is: if i say mysort:bubblesort([2,1,3,4,5]). the output is as i would expect: [1,2,3,4,5]

but if i say mysort:bubblesort([2,1,3,5,4]). the output is: [1,2,3,5|4].

my only question is: what does this "|" sign mean in between the listitems ?!

thank you all!

解决方案

A list can have two forms: Either it is empty ([]), or it has a head and a tail ([H|T]). So [] is an empty list, [1 | []] is a list whose head is 1 and whose tail is [], and [1|2] is a list whose head is 1 and whose tail is 2.

A list is called a proper list if it is empty or its tail is a proper list. So [] is a proper list because its empty and [1|[]] is a proper list because its tail is the empty list (which is a proper list), but [1|2] is not a proper list because its tail is 2 and 2 is not a proper list.

Since proper lists are the most common types of lists and reading and writing them as nested lists like that is kind of cumbersome, there's a special syntax for them: A proper list is displayed and can be written by separating the heads of the sublists with commas. So if we have the proper list [1 | [2 | [3 | []]]] it is displayed as [1,2,3] (and we can also write it that way), which is much more readable.

This format is also used to display the beginning of improper lists if they "start out" proper up to the point where the non-proper tail is, which will be separated with a |. So for example if we have the improper list [1 | [2 | [3|4]]] (which is improper because the innermost tail is 4, which is not a proper list), it will be displayed as [1,2,3|4] to distinguish it from the proper list [1 | [2 | [ 3 | [4 | []]]]], which would be displayed as [1,2,3,4].

So if you see something like that, you somehow created a list whose tail is not a proper list.

这篇关于Erlang列表连接,奇怪的“|”标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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