Elm에서 튜플은 두 개 또는 세 개의 값을 담을 수 있고, 각 값은 어떤 타입이든 가질 수 있어요. Elm에서 튜플은 주로 단순하고 일시적인 타입에 사용해요. 그래서 튜플은 최대 3개의 값까지만 가질 수 있고, 더 나아가 편의 함수는 2개의 값에 대해서만 제공해요. 더 복잡한 데이터에는 레코드로 넘어가는 게 좋아요.
type alias IntStringTuple = (Int, String)
aTuple : IntStringTuple
aTuple = (123, "elements can be of differing types")
anotherTuple : (String, String)
anotherTuple = Tuple.pair "this time it's" "two strings"
--> ("this time it's", "two strings")
Tuple.first aTuple
--> 123
Tuple.second aTuple
--> "elements can be of differing types"
튜플은 튜플 패턴 매칭을 사용해 바인딩에서 _구조 분해_할 수 있어요. 구조 분해는 let 식, 함수 선언, case 식에서 할 수 있어요.
repeatString : (Int, String) -> Bool
repeatString aTuple =
let
(aNumber, aString) = aTuple
in
String.repeat aNumber aString
repeatStringAgain : (Int, String) -> Bool
repeatStringAgain (aNumber, aString) = String.repeat aNumber aString
has123 : (Int, String, Bool) -> Bool
has123 aTuple =
case aTuple of
(123, _, _) ->
True
(_, "123", _) ->
True
(_, _, True) ->
True
_ ->
False
Aazra와 Rui는 해적을 테마로 한 보물찾기를 설계하고 있어요. 한쪽에는 지도상의 위치와 함께 보물 목록이 있고, 다른 쪽에는 지도상의 위치와 함께 장소 이름 목록이 있어요.
| 보물 | 위치 |
|---|---|
| Amethyst Octopus | (1, F) |
| Angry Monkey Figurine | (5, B) |
| Antique Glass Fishnet Float | (3, D) |
| Brass Spyglass | (4, B) |
| Carved Wooden Elephant | (8, C) |
| Crystal Crab | (6, A) |
| Glass Starfish | (6, D) |
| Model Ship in Large Bottle | (8, A) |
| Pirate Flag | (7, F) |
| Robot Parrot | (1, C) |
| Scrimshaw Whale's Tooth | (1, F) |
| Silver Seahorse | (4, E) |
| Vintage Pirate Hat | (7, E) |
| 장소 이름 | 위치 | 사분면 |
|---|---|---|
| Seaside Cottages | (C, 1) | Blue |
| Aqua Lagoon (Island of Mystery) | (F, 1) | Yellow |
| Deserted Docks | (A, 2) | Blue |
| Spiky Rocks | (D, 3) | Yellow |
| Abandoned Lighthouse | (B, 4) | Blue |
| Hidden Spring (Island of Mystery) | (E, 4) | Yellow |
| Stormy Breakwater | (B, 5) | Purple |
| Old Schooner | (A, 6) | Purple |
| Tangled Seaweed Patch | (D, 6) | Orange |
| Quiet Inlet (Island of Mystery) | (E, 7) | Orange |
| Windswept Hilltop (Island of Mystery) | (F, 7) | Orange |
| Harbor Managers Office | (A, 8) | Purple |
| Foggy Seacave | (C, 8) | Purple |
플레이어는 지도를 돌아다니면서 가는 곳마다 보물을 주워요. 보물 위치와 장소 위치는 형식이 서로 달라서, 게임에 한층 더 도전적인 요소를 더해줘요. 게임에 또 다른 재미를 더하기 위해, 특정 보물을 다른 보물로 바꿀 수 있는 특별한 장소도 몇 군데 있어요. 이제 이 게임을 도와줄 함수를 작성해 봐요.
placeLocationToTreasureLocation 함수를 구현해 봐요. 이 함수는 장소 위치(예: ('C', 1))를 받아서 보물 위치(예: (1, 'C'))로 변환해요.
placeLocationToTreasureLocation ('C', 1)
--> (1,'C')
treasureLocationMatchesPlaceLocation 함수를 구현해 봐요. 이 함수는 PlaceLocation(예: ('C', 1))을 받아서 TreasureLocation(예: (1, 'C'))과 일치하면 true를 반환해요.
treasureLocationMatchesPlaceLocation ('C', 1) (1, 'C')
--> true
treasureLocationMatchesPlaceLocation ('C', 1) (2, 'C')
--> false
countPlaceTreasures 함수를 구현해 봐요. 이 함수는 장소(예: ("Aqua Lagoon (Island of Mystery)", ('F', 1)))와 보물 목록을 받아서, 그곳에서 찾을 수 있는 보물의 개수를 반환해요.
place = ("Aqua Lagoon (Island of Mystery)", ('F', 1))
countPlaceTreasures place treasures =
--> 2
specialCaseSwapPossible 함수를 구현해 봐요. 이 함수는 보물(예: ("Amethyst Octopus", (1, 'F')))과 장소(예: ("Seaside Cottages ", ('C', 1)))를 받아서 다음 조합에 대해 true를 반환해요:
이 함수는 if문을 쓰는 대신, 즉석에서 튜플을 만들어 case문으로 구현해 봐요. Elm에서 흔히 쓰는 방식이에요.
Exercism에 가입하고 Elm 트랙을 개념 28개연습 문제 110개, 그리고 실제 사람의 멘토링과 함께 배우고 익혀 보세요. 모두 무료예요.