Elm-list

提供:Dev Guides
移動先:案内検索

ニレ-リスト

List、Tuples、およびRecordデータ構造を使用して、値のコレクションを保存できます。

この章では、Elmでリストを使用する方法について説明します。

リストは同種の値のコレクションです。 リスト内の値はすべて同じデータ型である必要があります。

変数を使用して値を保存する際には、次の制限を考慮してください-

  • 変数は本質的にスカラーです。 つまり、宣言時には、変数は1つの値しか保持できません。 つまり、プログラムにn個の値を格納するには、n個の変数宣言が必要になります。 したがって、値のより大きなコレクションを格納する必要がある場合、変数の使用は実行できません。
  • プログラム内の変数には、ランダムな順序でメモリが割り当てられるため、宣言の順序で値を取得または読み取ることが難しくなります。

構文

List_name = [value1,value2,value3.....valuen]

次の例は、Elmでリストを使用する方法を示しています。 elm REPLでこの例を試してください-

> myList1 = [10,20,30]
[10,20,30] : List number
> myList2 = ["hello","world"]
["hello","world"] : List String

異なる型の値をリストに追加しようとすると、コンパイラは型の不一致エラーをスローします。 これを以下に示します。

> myList = [1,"hello"]
-- TYPE MISMATCH
---------------------------------------------
repl-temp-000.elm

The 1st and 2nd entries in this list are different types of values.

4| [1,"hello"]
^^^^^^^
The 1st entry has this type:
   number
But the 2nd is:
   String

リスト操作

次の表は、リストの一般的な操作を示しています-

Sr. No Method Description
1 isEmpty : List a → Bool checks if list is empty
2 reverse : List a → Bool reverses input list
3 length : List a → Int returns size of the list
4 maximum : List comparable → Maybe.Maybe comparable returns maximum value
5 minimum : List comparable → Maybe.Maybe comparable returns minimum value
6 sum : List number → number returns sum of all elements in list
7 product : List number → number checks if list is empty
8 sort : List comparable → List comparable sorts list in ascending order
9 concat : List (List a) → List a merges a bunch of list into one
10 append : List a → List a → List a merges two lists together
11 range : Int → Int → List Int returns a list of numbers from start to end
12 filter : (a → Bool) → List a → List a filters list of values from input list
13 head : List a → Maybe.Maybe a returns the first element from list
14 tail : : List a → Maybe.Maybe (List a) returns all elements except the head

isEmpty

リストが空の場合、この関数はtrueを返します。

構文

List.isEmpty list_name

関数の署名を確認するには、elm REPLに次のように入力します-

> List.isEmpty
<function> : List a -> Bool

> List.isEmpty
<function> : List a -> Bool

> List.isEmpty [10,20,30]
False : Bool

この関数はリストを逆にします。

構文

List.reverse list_name

関数の署名を確認するには、elm REPLに次のように入力します-

> List.reverse
<function> : List a -> List a

> List.reverse [10,20,30]
[30,20,10] : List number

長さ

この関数は、リストの長さを返します。

構文

List.length list_name

関数の署名を確認するには、elm REPLに次のように入力します-

> List.length
<function> : List a -> Int

> List.length [10,20,30]
3 : Int

最大

この関数は、空でないリストの最大要素を返します。

構文

List.maximum list_name

関数の署名を確認するには、elm REPLに次のように入力します-

> List.maximum
<function> : List comparable -> Maybe.Maybe comparable

> List.maximum [10,20,30]
Just 30 : Maybe.Maybe number
> List.maximum []
Nothing : Maybe.Maybe comparable

最小

この関数は、空でないリストの最小要素を返します。

構文

List.minimum list_name

関数の署名を確認するには、elm REPLに次のように入力します-

> List.minimum
<function> : List comparable -> Maybe.Maybe comparable

> List.minimum [10,20,30]
Just 10 : Maybe.Maybe number

sum

この関数は、リスト内のすべての要素の合計を返します。

構文

List.sum list_name

関数の署名を確認するには、elm REPLに次のように入力します-

> List.sum
<function> : List number -> number

> List.sum [10,20,30]
60 : number

製品

この関数は、リスト内のすべての要素の積を返します。

構文

List.product list_name

関数の署名を確認するには、elm REPLに次のように入力します-

<function>  : List number ->  number

List.product [10,20,30]
6000 : number

sort

この関数は、リスト内の最低値から最高値にソートします。

構文

List.sort list_name

関数の署名を確認するには、elm REPLに次のように入力します-

> List.sort
<function> : List comparable -> List comparable

> List.sort [10,20,30]
[10,20,30] : List number

コンカット

この関数は、リストの束を単一のリストに連結します。

構文

List.concat [ [list_name1],[list_name2],[list_name3],.....[list_nameN] ]

関数の署名を確認するには、elm REPLに次のように入力します-

> List.concat
<function> : List (List a) -> List a

> List.concat [[: List number

追加する

この関数は、2つのリストをまとめます。

構文

List.append [list_name1] [list_name2]

関数の署名を確認するには、elm REPLに次のように入力します-

> List.append
<function> : List a -> List a -> List a

> List.append [10,20] [30,40]
[10,20,30,40] : List number

++演算子を使用して、リストを別のリストに追加することもできます。 これは以下の例に示されています-

> [10.1,20.2] ++ [30.3,40.4]
[10.1,20.2,30.3,40.4] : List Float

範囲

この関数は、すべての要素が1ずつ増加する数値のリストを作成します。 リストにあるべき最小値と最大値が関数に渡されます。

構文

List.range start_range end_range

関数の署名を確認するには、elm REPLに次のように入力します-

> List.range
<function> : Int -> Int -> List Int

> List.range 1 10
[1,2,3,4,5,6,7,8,9,10] : List Int

フィルタ

この関数は、入力リストから値のセットをフィルタリングします。 テストに合格した値のみを保持します。

構文

List.filter test_function input_list

関数の署名を確認するには、elm REPLに次のように入力します-

> List.filter
<function> : (a -> Bool) -> List a -> List a

次の例では、入力リストからすべての偶数をフィルタリングします

> List.filter (\n -> n%2==0) [10,20,30,55]
[10,20,30] : List Int

head

この関数は、入力リストから最初の要素を返します。

構文

List.head input_list

関数の署名を確認するには、elm REPLに次のように入力します-

> List.head
<function> : List a -> Maybe.Maybe a

> List.head [10,20,30,40]
Just 10 : Maybe.Maybe number
> List.head []
Nothing : Maybe.Maybe a

tail

この関数は、リストの最初以降のすべての要素を返します。

構文

List.tail input_list

関数の署名を確認するには、elm REPLに次のように入力します-

> List.tail
<function> : List a -> Maybe.Maybe (List a)

> List.tail [10,20,30,40,50]
Just [20,30,40,50] : Maybe.Maybe (List number)
> List.tail [10]
Just [] : Maybe.Maybe (List number)
> List.tail []
Nothing : Maybe.Maybe (List a)

短所演算子の使用

cons演算子(::)は、リストの先頭に要素を追加します。

> 10::[20,30,40,50]
[10,20,30,40,50] : List number

追加する新しい要素とリスト内の値のデータ型は一致する必要があります。 データ型が一致しない場合、コンパイラはエラーをスローします。

> [1,2,3,4]::[5,6,7,8]
-- TYPE MISMATCH ---------------------------------
------------ repl-temp-000.elm

The right side of (::) is causing a type mismatch.

3| [1,2,3,4]::[5,6,7,8]
              ^^^^^^^^^
(::) is expecting the right side to be a:

   List (List number)

But the right side is:

   List number
Hint: With operators like (::) I always check the left side first. If it seems fine,
I assume it is correct and check the right side. So the
problem may be in how the left and right arguments interact.

リストは不変です

リストがElmで不変かどうかを確認しましょう。 最初のリスト_myList_は、値1と連結されたときに新しいリストを作成し、_myListCopy_に返されます。 したがって、初期リストを表示する場合、その値は変更されません。

> myList = [10,20,30]
[10,20,30] : List number
> myListCopy = 1::myList
[1,10,20,30] : List number
> myList
[10,20,30] : List number
>myList == myListCopy
False : Bool