在机器学习中,可以为匹配模式的每个元素分配名称:
fun findPair n nil = NONE
| findPair n (head as (n1, _))::rest =
if n = n1 then (SOME head) else (findPair n rest)
在此代码中,我为列表的第一对定义了一个别名,并匹配该对的内容。 Scala 中有等效的构造吗?
最佳答案
你可以做variable binding带有 @
符号,例如:
scala> val wholeList @ List(x, _*) = List(1,2,3)
wholeList: List[Int] = List(1, 2, 3)
x: Int = 1
关于scala - Scala 是否有相当于 ML 的 "as"构造的语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205726/