Haskell - 模式同义词在记录更新中的使用

max*_*loo 5 haskell

我现在正在尝试一段代码:

https://mpickering.github.io/posts/2015-12-12-pattern-synonyms-8.html

{-# LANGUAGE PatternSynonyms #-}
pattern MyPoint :: Int -> Int -> (Int, Int)
pattern MyPoint{m, n} = (m,n)
m :: (Int, Int) -> Int
n :: (Int, Int) -> Int
test9 = (0,0) { m = 5 }
Run Code Online (Sandbox Code Playgroud)

但是test9抛出一个错误:

• ‘m’ is not a record selector
• In the expression: (0, 0) {m = 5}
Run Code Online (Sandbox Code Playgroud)

我如何test9上班?

Apl*_*123 8

当你写:

m :: (Int, Int) -> Int
n :: (Int, Int) -> Int
Run Code Online (Sandbox Code Playgroud)

您将mand声明n为函数,它们失去了作为记录选择器的特殊位置。只需完全删除这些行:

{-# LANGUAGE PatternSynonyms #-}
pattern MyPoint :: Int -> Int -> (Int, Int)
pattern MyPoint{m, n} = (m,n)
test9 = (0,0) { m = 5 } -- (5, 0)
Run Code Online (Sandbox Code Playgroud)