【发布时间】:2021-09-08 20:39:34
【问题描述】:
我想了解如何在不使用记录语法的情况下访问自定义数据类型的字段。在 LYAH 中建议这样做:
-- Example
data Person = Subject String String Int Float String String deriving (Show)
guy = Subject "Buddy" "Finklestein" 43 184.2 "526-2928" "Chocolate"
firstName :: Person -> String
firstName (Subject firstname _ _ _ _ _) = firstname
我尝试通过获取 BST 节点的值来应用这种访问数据的方式:
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)
singleton :: a -> Tree a
singleton x = Node x EmptyTree EmptyTree
treeInsert :: (Ord a) => a -> Tree a -> Tree a
treeInsert x EmptyTree = singleton x
treeInsert x (Node a left right)
| x == a = Node x left right
| x < a = Node a (treeInsert x left) right
| x > a = Node a left (treeInsert x right)
getValue :: Tree -> a
getValue (Node a _ _) = a
但我收到以下错误:
有人可以解释如何在不使用记录语法的情况下正确访问该字段和错误消息的含义是什么?请注意,我是 Haskell 的初学者。我的目的是了解为什么这种特殊情况会引发错误以及如何正确地做到这一点。我不是要这指向更方便的访问字段的方法(如记录语法)。如果之前有人问过类似的问题:对不起!我真的试着在这里找到答案,但不能。任何帮助表示赞赏!
【问题讨论】:
-
您应该复制粘贴错误的文本。
-
我知道的其中之一。
标签: haskell types functional-programming field