๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“  archive

[Haskell Tutorial] Data Type

by HandHand 2022. 12. 31.

 

๐Ÿ“Œ data ํƒ€์ž…

Haskell ์—์„œ ํƒ€์ž…์„ ์ •์˜ํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” data ์ž…๋‹ˆ๋‹ค.

 

data <type-name> <type-args>
    = <type-constructor> <type>
  | <type-constructor> <type>
  | <type-constructor> <type>
  | ...

 

newtype ๊ณผ ๋‹ค๋ฅด๊ฒŒ ์—ฌ๋Ÿฌ ํƒ€์ž…์ธ์ž๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ ,

์—ฌ๋Ÿฌ constructor ๋ฅผ ํ•˜๋‚˜์˜ ํƒ€์ž…์— ๋งค์นญ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

constructor ๋’ค์— ํƒ€์ž…์ธ์ž๋Š” 0๊ฐœ ์ด์ƒ์„ ์ •์˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ data ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

 

data Bool = True | False

 

์–ด์งธ ํ˜•ํƒœ๊ฐ€ ์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ ์‚ฌ์šฉํ•˜๋Š” enum ๊ณผ ๋งŽ์ด ๋‹ฎ์•„์žˆ์ง€์•Š๋‚˜์š”?

๋˜๋‹ค๋ฅธ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

data Person = Person String Int -- ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” Person ํƒ€์ž…

 

์œ„๋Š” ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” Person ํƒ€์ž…์„ ์ •์˜ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์œ„์™€ ๊ฐ™์ด ํƒ€์ž…์„ ์ •์˜ํ•  ๊ฒฝ์šฐ ๊ฐ๊ฐ์˜ ํƒ€์ž…์ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๋ช…ํ™•ํ•˜์ง€ ์•Š๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹จ์ˆœํ•œ ํƒ€์ž…์ด๋ผ๋ฉด ๋ฌธ์ œ๊ฐ€ ์•ˆ๋˜๊ฒ ์ง€๋งŒ, ๋ณต์žกํ•ด์งˆ์ˆ˜๋ก ํŒŒ์•…์ด ํž˜๋“ค ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด๋Ÿด๋•Œ record syntax ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ช…ํ™•ํ•œ ํƒ€์ดํ•‘์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

 

๐Ÿ’ก Record Syntax

record syntax ๋Š” constructor ๊ฐ๊ฐ์˜ ํƒ€์ž…์— ํ•„๋“œ๋ฅผ ๋ถ™์ผ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

์ฃผ์˜ํ•  ์ ์€ ํ•„๋“œ์ด๋ฆ„ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ,

์ด๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๋ณดํ†ต constructor ๋ช…์„ prefix ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

data Person = Person { personName :: String, personAge :: Int }

{- โœ… ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -}

person = Person { personName = "owen", personAge = 28 }

 

Haskell ์€ ๋˜ํ•œ ํ•„๋“œ๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

์œ„ ์˜ˆ์‹œ์—์„œ ๋‚˜์ด ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

ghci > owen = Person { personName = "owen", personAge = 28  }
ghci > personAge owen // โœ… personAge ํ•„๋“œ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์ž๋™์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
28

 

record syntax ์—๋Š” ํ•„๋“œ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋„ ์ œ๊ณตํ•ด์ฃผ๋Š”๋ฐ,

์ด๋•Œ ์›๋ณธ์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

ghci > personAge (owen { personAge = 20 })
20
ghci > personAge owen // โœ… ์›๋ณธ์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
28

 

๐Ÿ“Œ Tuple & Either

data ๋กœ ์ •์˜๋œ ํƒ€์ž… ์ค‘ ๋จผ์ € ์‚ดํŽด๋ณผ๋งŒํ•œ ๋‘๊ฐ€์ง€ ํƒ€์ž… Tuple ๊ณผ Either ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1๏ธโƒฃ Tuple

๋จผ์ € Tuple ์€ ์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ ์•„๋Š” ํŠœํ”Œ ์ž๋ฃŒํ˜•๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Haskell ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํƒ€์ž…์ด ์ •์˜๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

data Tuple a b = Tuple a b

 

์—ฌ๊ธฐ์„œ a, b ๋Š” ๊ฐ๊ฐ ์ œ๋„ค๋ฆญ ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋Š ํƒ€์ž…์ด๋“  ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํŠœํ”Œ์€ ๊ณ ์ •๊ธธ์ด์˜ ์ž๋ฃŒํ˜•์ด๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ถ•์•ฝ ๋ฌธ๋ฒ•๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

Tuple "apple" "banana"

{- ๋‹ค์Œ์œผ๋กœ ์ถ•์•ฝํ‘œํ˜„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. -}

("apple", "banana")

 

2๏ธโƒฃ Either

Either ์€ ๋•Œ์— ๋”ฐ๋ผ์„œ ๋‘๊ฐ€์ง€ ํƒ€์ž… ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•  ๊ฒฝ์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํ”ํžˆ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋•Œ ์ด ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•œ๋ฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

data Either a b = Left a | Right b

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒจํ„ด๋งค์นญ๊ณผ ํ•จ๊ป˜ ํ™œ์šฉํ•˜๋ฉด ์—๋Ÿฌ ํ•ธ๋“ค๋ง์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

case response of
    Left error -> {- ๐Ÿšจ error case -}
    Right result -> {- โœ… 200 response -}

 

๐Ÿ“Œ ์ฐธ๊ณ ์ž๋ฃŒ

Haskell Data Types in 5 Steps - Monday Morning Haskell

๋ฐ˜์‘ํ˜•

'๐Ÿ“  archive' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Haskell Tutorial] Recursion  (0) 2022.12.31
[Haskell Tutorial] High Order Function  (0) 2022.12.31
[Haskell Tutorial] Module  (0) 2022.11.28
[Haskell Tutorial] Type  (0) 2022.11.28
[Haskell Tutorial] Function & Chaining  (0) 2022.11.28

๐Ÿ’ฌ ๋Œ“๊ธ€