dca*_*tro 5 functional-programming record elm
说我有这个记录:
type alias Rec = { a : Int }
Run Code Online (Sandbox Code Playgroud)
并且,例如,一个函数,它接受其中两个并求和它们的整数.
f: Rec -> Rec -> Int
Run Code Online (Sandbox Code Playgroud)
这可以使用记录访问器(即f x y = x.a + y.a)实现,但有没有办法使用模式匹配来提取两个整数?
显然,这两个不起作用,因为它们将两个不同的数字绑定到同一个变量:
f {a} {a} = a + a
f x y = case (x, y) of ({a}, {a}) -> a + a
Run Code Online (Sandbox Code Playgroud)
目前没有办法做到这一点。存在模式别名 ( as),但它仅适用于整个模式,因此这是无效的:
type alias Rec = { a : Int }
f: Rec -> Rec -> Int
f { a as xa } { a as ya } = xa + ya
main = f { a = 1 } { a = 2 }
Run Code Online (Sandbox Code Playgroud)
结果是:
Detected errors in 1 module.
-- SYNTAX PROBLEM --------------------------------------------------------------
I ran into something unexpected when parsing your code!
4| f { a as xa } { a as ya } = xa + ya
^
I am looking for one of the following things:
a closing bracket '}'
whitespace
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
187 次 |
| 最近记录: |