Erlang: a List in disorder _ PHP Tutorial

Source: Internet
Author: User
Erlang unordered List scheme. I recently read erlang systematically. now I use erlangprogramming as the learning material. after reading the first seven or eight chapters, I tried to write something and found that I was helpless, you can find help materials and documents, and recently read erlang systematically. Currently, erlang programming is used as the learning material. after reading the first seven or eight chapters, I tried to write something and found that I was at a loss, there are very few materials and documents that can help.

In fact, I hope someone can study or discuss it together.

Today's question: a List in disorder.

Method 1: a more sophisticated disordered solution.

[Php]

-Module (shuffle ).

-Export ([do/1]).

Do (L)->

Len = length (L ),

NL = lists: map (fun (X)-> {random: uniform (Len), X} end, L ),

NLL = lists: sort (NL ),

[V | {_, V} <-NLL].

In fact, this problem is quite confusing to me. Once a variable is assigned a value, it cannot be modified. This feature makes the idea and idea of writing a program completely different. My above practice is to use a random number to generate a list like [{Rand1, Elem1},..., {RandN, ElemN.

Then, sort it by sort and print the Elem again. The unordered result is achieved.

The result is as follows:

[Php]

45> c (shuffle ).

{OK, shuffle}

46> shuffle: do ([1, 2, 3, 4, 5]).

[4, 5, 1, 2, 3]

47> shuffle: do ([1, 2, 3, 4, 5]).

[2, 1, 5, 3, 4]

48> shuffle: do ([1, 2, 3, 4, 5]).

[3, 1, 2, 4, 5]

49> shuffle: do ([1, 2, 3, 4, 5]).

[1, 5, 2, 3, 4]

50> shuffle: do ([1, 2, 3, 4, 5]).

[5, 1, 4, 3, 2]

51>

Method 2: General shuffling algorithm

Next, a List in disorder. I think the original general shuffling algorithm is feasible. I tried to write another function. The test is effective, but I think there is definitely room for optimization. The style of the code still reveals a strong procedural ideology.

% Shuffling algorithm scheme

[Php]

Do2 (L)->

Do2 (L, []).

Do2 ([], L)->

L;

Do2 (L1, L2)->

% Io: format ("L1 = ~ W L2 = ~ W ~ N ", [L1, L2]),

Len = length (L1 ),

If

Len> 1->

NL = lists: split (random: uniform (Len-1), L1 ),

{[H1 | T1], [H2 | T2]} = NL,

NL2 = lists: flatten ([T1], [H1 | T2]),

L11 = lists: append (L2, [H2]),

Do2 (NL2, L11 );

True->

Do2 ([], lists: append (L2, L1 ))

End.

The result is:

128> c (shuffle ).

{OK, shuffle}

129> shuffle: do2 (lists: seq )).

[9, 2, 5, 1, 8, 0, 7, 3, 6, 4]

130> shuffle: do2 (lists: seq )).

[8, 3, 6, 5, 7, 4, 9, 0, 2, 1]

131> shuffle: do2 (lists: seq )).

[,]

132> shuffle: do2 (lists: seq )).

[,]

133>

There are several points worth thinking about whether these two solutions are true shuffling algorithms. Is it equal to the probability that each number appears at a certain position.

Other methods are not supported.

Explain programming is the learning material. after reading the first seven or eight chapters, I tried to write something and found that I was helpless. I could find help materials and documents...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.