學習軌道
/
Elm
Elm
/
練習
/
Tisbury 尋寶之旅
Tisbury 尋寶之旅

Tisbury 尋寶之旅

學習練習

簡介

元組

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 正在設計一場海盜主題的尋寶遊戲。其中有一份帶著地圖座標的寶藏陣列,還有一份帶著地圖座標的地點名稱陣列。

Treasure Location
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)
Place Name Location Quadrant
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

玩家會在地圖上四處移動,一邊走一邊撿拾寶藏。 寶藏和地點的座標格式刻意不同,好增加一點挑戰性。 另外還有一些特殊地點,在那裡特定的寶藏可以兌換成其他特定的寶藏,為遊戲增添額外的變化。 你的任務就是撰寫函式,幫他們完成這款遊戲。

1. Convert locations

實作 placeLocationToTreasureLocation 函式,它接受一個地點座標(例如 ('C', 1)),並將其轉換成寶藏座標(例如 (1, 'C'))。

placeLocationToTreasureLocation ('C', 1)
--> (1,'C')

2. Compare Treasure and Place locations

實作 treasureLocationMatchesPlaceLocation 函式,它接受一個 PlaceLocation(例如 ('C', 1)),如果符合 TreasureLocation(例如 (1, 'C')),就回傳 true。

treasureLocationMatchesPlaceLocation ('C', 1) (1, 'C')
--> true

treasureLocationMatchesPlaceLocation ('C', 1) (2, 'C')
--> false

3. Count Treasure Locations

實作 countPlaceTreasures 函式,它接受一個地點(例如 ("Aqua Lagoon (Island of Mystery)", ('F', 1)))和寶藏陣列,並回傳在該地點能找到的寶藏數量。

place = ("Aqua Lagoon (Island of Mystery)", ('F', 1))

countPlaceTreasures place treasures =
--> 2

4. Special Places

實作 specialCaseSwapPossible 函式,它接受一個寶藏(例如 ("Amethyst Octopus", (1, 'F')))和一個地點(例如 ("Seaside Cottages ", ('C', 1))),並在下列組合中回傳 true:

  • Brass Spyglass 可以在 Abandoned Lighthouse 兌換成任何其他寶藏
  • Amethyst Octopus 可以在 Stormy Breakwater 兌換成 Crystal Crab 或 Glass Starfish
  • Vintage Pirate Hat 可以在 Harbor Managers Office 兌換成 Model Ship in Large Bottle 或 Antique Glass Fishnet Float

試著把這個函式實作成帶有臨時元組的 case 敘述,而不要使用 if 敘述(這是 Elm 中常見的慣用寫法)。

透過 GitHub 編輯 連結會在新視窗或分頁中開啟
Elm Exercism

準備好開始 Tisbury 尋寶之旅 了嗎?

註冊 Exercism,透過 28 個概念110 個練習 和真人引導來學習並精通 Elm,全部免費。