Skip to content

Commit b132fa1

Browse files
committed
Example function to pattern-match against a record type
1 parent b63fc84 commit b132fa1

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

assets/18.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -845,11 +845,11 @@ data Entry =
845845
f (Entry cat pri) = {- ...ここで category が入った cat を、と price が入った pri を使う式 ... -}
846846
```
847847

848-
hoge
848+
上記の例は、値コンストラクター`Entry`の第一引数にあたる`category``cat`という変数に、第二引数にあたる`price``pri`という変数に、それぞれパターンマッチで代入する例です。このように書くことで関数の本体、`{- ...ここで category が入った cat を、と price が入った pri を使う式 ... -}`の箇所で`cat``pri`を使えるようになります。
849849

850-
値コンストラクター`Entry`の第一引数にあたる`category`、第二引数にあたる`price`、それぞれにパターンマッチできる!
850+
実際に`Entry`型の値を受け取って、その`category``price`にパターンマッチする関数を書いてみましょう:
851851

852-
```
852+
```haskell
853853
ghci> :{
854854
ghci| formatEntry :: Entry -> String
855855
ghci| formatEntry (Entry cat pri) = cat ++ ": " ++ show pri

0 commit comments

Comments
 (0)