2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

草莽Erlang ── 16. lists:seq

Last updated at Posted at 2023-01-08

口で言うより行うことがErlang習得への近道と信じています。

lists:seq

seq(integer, integer) -> [integer]
seq(範囲開始値, 範囲終了値) -> 昇順の等差数列

seq(integer, integer, integer) -> [integer]
seq(範囲開始値, 範囲終了値, 増分) -> 昇順の等差数列

指定された範囲の連番や昇順の等差数列を生成できます。逆順(降順)には対応できません。

練習してみます。

> lists:seq(1, 5).
[1,2,3,4,5]

> lists:seq(1, 5, 2).
[1,3,5]

> lists:seq($A, $A + 5).
"ABCDEF"

> lists:seq($a, $a + 5).
"abcdef"

% 増分が負の数なのでエラー
> lists:seq(1, 5, -1).
** exception error: bad argument
     in function  lists:seq/3
        called as lists:seq(1,5,-1)
        *** argument 3: not a positive increment

% 範囲終了値が範囲開始値より小さいのでエラー
> lists:seq(5, 1).
** exception error: no function clause matching
                    lists:seq(5,1) (lists.erl, line 271)

% 内包表記を使った技
> [{X, X * 10} || X <- lists:seq(1, 5)].
[{1,10},{2,20},{3,30},{4,40},{5,50}]

listsモジュールには他にもリスト処理のための関数がたくさんあります。

Elixirにも挑戦したい

闘魂ElixirシリーズとElixir Schoolがオススメです。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?