Erlang入门: 几个简单小程序

栏目: Erlang · 发布时间: 8年前

内容简介:Erlang入门: 几个简单小程序

生成斐波那契数列

#! /usr/bin/env escript

main([A]) ->
    I=list_to_integer(A),
    F=fac_list(I),
    io:format("feribo ~w = ~w~n",[I,F]).

element(1) -> 1;
element(2) -> 1;
element(N) -> element(N-1) + element(N-2).

fac_list(N) -> fac_list([], N).

fac_list(L, 0) -> L;
fac_list(L, N) -> fac_list([element(N)|L], N-1).

运行结果

dingkaideMacBook-Pro:erlang dingkai$ ./feibo 25
feribo 25 = [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025]

求平均数

#! /usr/bin/env escript

%main([L]) ->
main([]) ->
    L = [1, 2, 3],
    io:format("list = ~w~n", [L]),
    Avgrage = count_average(L),
    io:format("avrage of ~w = ~w~n",[L,Avgrage]).

num([]) -> 0;
num([H|T]) -> 1+num(T).

sum([]) -> 0;
sum([H|T]) -> H + sum(T).

count_average([]) -> 0;
count_average(L) -> sum(L)/num(L).

运行结果

dingkaideMacBook-Pro:erlang dingkai$ ./avg
./avg:11: Warning: variable 'H' is unused
list = [1,2,3]
avrage of [1,2,3] = 2.0

求N个数的平方和

#! /usr/bin/env escript

main([A]) ->
    N = list_to_integer(A),
    SquareSum = sum_square(N),
    io:format("sum square ~w = ~w~n",[N,SquareSum]).

compute_square(X) -> X*X.

sum_square(1) -> compute_square(1);
sum_square(N) -> sum_square(N-1) + compute_square(N).

运行结果:

dingkaideMacBook-Pro:erlang dingkai$ ./square_sum 10
sum square 10 = 385

连接两个链表

将列表L1和L2连接起来,将L2的元素依次加入L1,代码如下:

#! /usr/bin/env escript

%main([A]) ->
main([]) ->
    L1 = [1, 2, 3],
    L2 = [4, 5, 6],
    L = concatenate(L1, L2),
    io:format("L1 + L2 = ~w~n",[L]),
    io:format("concat of [1, 2, 3] and [4, 5] = ~w~n", [concat([1, 2, 3], [4, 5])]).

%函数reverse将列表逆置
reverse(L) -> reverse(L, []).

reverse([], L) -> L;
reverse([H|T], L) -> reverse(T, [H|L]).

concat(L, []) -> L;
concat(L, [H|T]) -> concat([H|L], T).

concatenate(L1, L2) -> concat(L1, reverse(L2)).

运行结果:

dingkaideMacBook-Pro:erlang dingkai$ ./concat_lists
L1 + L2 = [4,5,6,1,2,3]

这个例子比较有趣的地方在于一个reverse函数和一个concat函数

reverse([H|T], L) -> reverse(T, [H|L]).

的作用是将将第一个列表[H|T]的第一个元素T拿出来放在L的第一位,其实是一个不断剥离原始列表首元素的过程。

举个例子来说,假如L为[1, 2, 3],那么

reverse(L) -> reverse(L, []).

其实就是

reverse([1, 2, 3], [])

而其计算过程是:

reverse([1, 2, 3], []) =>
reverse([2, 3], [1]) =>
reverse([3], [2, 1]) =>
reverse([], [3, 2, 1]) =>
[3, 2, 1]

concat的方法其过程则表达为下面的过程:

concat(L, [H|T]) -> concat([H|L], T)

这就是将第二个列表[H|T]中不断剥离首个元素并插入到L的前面,直到[H|T]为空为止。

例如:contact([1, 2, 3], [4, 5])过程如下:

contact([1, 2, 3], [4, 5]) =>
contact([4, 1, 2, 3], [5]) =>
contact([5, 4, 1, 2, 3], []) =>
[5, 4, 1, 2, 3]

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

500 Lines or Less

500 Lines or Less

Amy Brown、Michael DiBernardo / 2016-6-28 / USD 35.00

This book provides you with the chance to study how 26 experienced programmers think when they are building something new. The programs you will read about in this book were all written from scratch t......一起来看看 《500 Lines or Less》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具