Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing named fields in a Haskell function

Tags:

haskell

ghci

I've defined a Tree data type in Haskell and an associated 'size' method which calculates the number of elements in the tree. This worked before, however I have updated the Tree data type to use named fields as in the following definition:

data Tree a = Empty
 | Leaf {value::a}
 | Node {left :: (Tree a), value :: a, right :: (Tree a)}
 deriving (Eq, Ord, Show)

I've found (by playing around in GHCi) that I can access a named field using the function (left n) for example. However, when I try to use this function I get an error:

size :: Tree a -> Int
size Empty    = 0
size (Leaf l)   = 1
size (Node n)   = size (left n) + 1 + size (right n)

GHCi simply says "Not in scope: left" and the equivalent for right. The Tree definition is in a module called Tree and the size definition is in a module called Main, but with the non-named fields I never had a problem with scope when it came to accessing variables from the Tree datatype.

like image 589
benwad Avatar asked Nov 15 '25 13:11

benwad


1 Answers

Your size (Node n) pattern needs to have a correct constructor. In your code n extracts the left element.

Try this (if you really want to use the named patterns):

size n@(Node _ _ _)   = size (left n) + 1 + size (right n)

Or even:

size n@(Node {})   = size (left n) + 1 + size (right n)

Or you can extract the labels in the pattern:

size (Node {left=l, right=r})   = size l + 1 + size r

or simply ignore the labels:

size (Node l _ r)   = size l + 1 + size r
like image 161
0xfe Avatar answered Nov 17 '25 09:11

0xfe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!