轨道
/
Elm
Elm
/
练习
/
Blorkemon 卡牌
Blorkemon 卡牌

Blorkemon 卡牌

学习练习

简介

比较

相等性

Elm 中可以用相等运算符 (==)检查两个值是否“相同”。 不等运算符是 (/=)(不是许多语言里的 !=)。 相等运算符可用于字面量、元组、记录和自定义类型。

1 == 2
    --> False

1 /= 2
    --> True

(1, 2) == (2, 1)
    --> False

type alias MyRecord = { myInt : Int, myStrings : List String }
a : MyRecord
a = MyRecord (1 + 1) ["hello world"]

type alias MyOtherRecord = { myInt : Int, myStrings : List String }
myStrings =  (String.join " " ["hello", "world"]) :: []
b : MyOtherRecord
b = { myInt = 2, myStrings = myStrings}

a == b
    --> True

注意,绝对不要比较函数,否则程序会在运行时崩溃。

比较

比较运算符 (<)、(<=)、(>)、(>=) 以及函数 min、max、compare 都作用于 comparable类型。 comparable是一种特殊类型,它把所有可比较的内置类型归为一组:数字、字符、字符串、由可比较的值构成的列表,以及由可比较的值构成的元组。

1 < 1 + 1
    --> True

'x' < 'X'
    --> False

"abc" < "abz"
    --> True

"abc" < "abcd"
    --> True

"abc" < "b"
    --> True

min [1 , 2, 9000] [10]
    --> [1 , 2, 9000]

min (1, "hello") (3, "bye")
    --> (1, "hello")

记录或自定义类型等其他类型的值无法直接比较。

有些内置类型要求其内容必须是comparable,例如Set或Dict的键,因为它们的数据结构依赖于内部的排序。

由comparable值构成的列表可以用List.sort排序;而值可以映射为可比较值的列表,则可以用List.sortBy排序。 如果你需要多级排序(先按一个属性排序,再用另一个属性打破并列),也可以把元组或列表传给List.sortBy,因为元组和列表按字典序排序。

List.sort ["hi", "hello", "bye", "goodbye"]
    --> ["bye", "goodbye", "hello", "hi"]

List.sortBy String.length ["hi", "hello", "bye", "goodbye"]
    --> ["hi", "bye", "hello", "goodbye"]

-- sort by length, then alphabetically
List.sortBy (\str -> (String.length str, str)) ["hi", "mum", "hello", "sis", "bye", "dad"]
    --> ["hi", "bye", "dad", "mum", "sis", "hello"]

函数compare接收两个comparable值,返回一个Order。 Order是一种类型,它通过一次操作就能判断一个值是大于(GT)、等于(EQ)还是小于(LT)另一个值。

compare 1 2
    --> LT

compare "" ""
    --> EQ

compare [12] []
    --> GT

可以为任意类型构造比较函数,例如把它传给List.sortWith进行排序:

type Color = Red | Green | Blue

compareColors : Color -> Color -> Order
compareColors a b =
  case (a, b) of
    (Red, Red) -> EQ
    (Red, _) -> LT
    (_, Red) -> GT
    (Green, Green) -> EQ
    (Green, _) -> LT
    (_, Green) -> GT
    (Blue, Blue) -> EQ

List.sortWith compareColors [Blue, Red, Green, Green, Blue]
    --> [Red, Green, Green, Blue, Blue]

说明

你突然涌起一阵怀旧之情,特别想把那盒旧 Blorkemon™️ 卡牌翻出来掸掸灰。 Blorkemon™️ 卡牌是一款非常棒的老派集换式卡牌游戏。 每张Card上都画着一只 Blorkemon™️ 怪兽,还标注了攻击力等级;运气好的话,这张卡甚至还是闪卡。

1. 比较力量值

每张 Blorkemon™️ 卡牌看着都比下一张更强,不过最好还是亲自比一比。

实现isMorePowerful函数。如果第一个实参所代表的卡牌严格强于另一张,就返回True,否则返回False。

newthree = Card "Newthree" 120 False
scientuna = Card "Scientuna" 6 True

isMorePowerful newthree scientuna
    --> True

2. 找出最高的力量值

手上一把 Blorkemon™️ 卡牌,你应该能凑出最具毁灭性的一击。

实现maxPower函数,返回两张卡中较高的力量值。

maxPower newthree scientuna
    --> 120

3. 给卡牌排序

你依稀记得每种 Blorkemon™️ 你都至少有一张,不过距离上次清点已经过去很久了;你该把卡牌整理排序,好跟 Pulpapedia 上的官方图鉴对照。

实现sortByMonsterName函数,它接收一个Cards列表,返回按怪兽名字排好序的列表。

sortByMonsterName [newthree, scientuna]
    --> [Card "Newthree" 120 False, Card "Scientuna" 6 True]

4. 最酷的卡牌排前面

Blorkemon™️ 是世界上最酷的东西。 你说这话可不是随便说说,你有一套科学方法来证明它。

实现sortByCoolness函数,它会给一列卡牌排序,把最酷的排在前面。 一张卡的酷炫程度首先取决于它闪不闪:所有闪卡都比其他卡酷得多。 第二个因素是力量值,越高越酷。

sortByCoolness [newthree, scientuna]
    --> [Card "Scientuna" 6 True, Card "Newthree" 120 False]

5. 闪光力量

闪光可不只是为了好看,在两只力量值相同的 Blorkemon™️ 对战时,闪的那只总能获胜。 这就叫闪光力量。

实现compareShinyPower函数,把这一特性写进代码。 如果两张卡的力量值不同,它们的Order由力量值决定;如果力量值相同,那么闪卡更大。

compareShinyPower newthree scientuna
    --> GT

6. 来下注吧

玩 Blorkemon™️ 卡牌游戏时,什么都有可能发生,但更强的卡还是往往更容易赢。

实现expectedWinner函数,它按照compareShinyPower的排序,返回最有可能获胜的怪兽名字。 这个函数应该返回预期获胜怪兽的名字;如果双方对手的闪光力量相同,则返回“too close to call”。

expectedWinner newthree scientuna
    --> "Newthree"
通过 GitHub 编辑 链接将在新窗口或新标签页中打开
Elm Exercism

准备好开始 Blorkemon 卡牌 了吗?

注册 Exercism,借助 28 个概念110 个练习 和真人导师指导,学习并掌握 Elm,全部免费。