大家都习惯用for循环打三角形,而Erlang恰恰没有for循环,看看如何使用Erlang打印三角呢?
代码语言:javascript复制43> [begin L=lists:duplicate(2*X-1,"*"),S=lists:duplicate(11-X," "),io:format(S L "~n") end|| X<-lists:reverse(lists:seq(1,11))].
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
[ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok]
44> [begin L=lists:duplicate(2*X-1,"*"),S=lists:duplicate(11-X," "),io:format(S L "~n") end|| X<-lists:seq(1,11)].
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
[ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok]
是不是更简单呢?
如果是简单的计数循环,更简单,例如:
代码语言:javascript复制52> [io:format("abc") || _<-lists:seq(1,10)].
abcabcabcabcabcabcabcabcabcabc[ok,ok,ok,ok,ok,ok,ok,ok,ok,ok]
- Previous Ubuntu 12.10 中自定义DNS服务器设置
- Next Erlang & Unicode