Couldn't match expected type 'Tree' with actual type '(Tree, Tree)' I don't understand why it should be

Asked 2 years ago, Updated 2 years ago, 405 views

Algebraic data types are expressed in tree structure, but I'm not sure what kind of structure they will be, so I'm posting another question.

When I wrote the above code,

The error is

is now

The error is

That's what happened.In the first place, as mentioned in the Answer, placing Empty on the edge of the tree structure is strange, so I think this error is useful, but Node(Leaf 200) (Leaf 300) is allowed and Node(Empty, Empty) doesn't understand the compilation error.Do you have a poor understanding of constructors?

It's a different story from the error, but

x=Node(Empty) -- OK

Tree I don't think it's the way the author intended to use it.If it's Empty all of a sudden as shown below, it's very convincing.

w=Empty -- OK

I don't think Haskell's writing tool has much to do with it, but I'll write it down just in case.I am writing Haskell in Haskell on the Mac App Store.

haskell

2022-09-30 21:51

2 Answers

 -- This is a type error
z = Node (Empty, Empty)

In this way, the Node constructor passes the tuple (Empty, Empty) as the only argument.z=Node(Empty, Empty) if you put a space for clarity.This error occurs because the Tree type section is passed where the Tree type section should be passed.

A side story

In this type of Tree used, the Node constructor and the Leaf constructor are trying to represent a tree with a node with a leaf value and cannot represent a tree without a node.We have specifically deployed the Empty constructor to make this adjustment.However, as a result, even items that are not meaningful to a tree have a Tree type, and items such as Node Empty also have a type.

There are several ways to prevent this inconsistency.For example, how to distinguish between a tree with a node and a tree without a node, although the expression is somewhat redundant.


2022-09-30 21:51

I'll answer myself after receiving your comment.

z=Node(Empty, Empty) -- NG

As the comment says, is not good

 (Empty, Empty)

The part of is considered a tuple and is not allowed. Correctly stated,

Node Empty Empty

Now

x=Node(Empty) -- OK

The reason why is OK can be tried with the code below.

data Tree=Empty
          | Leaf Int
          | Node Tree Delivery Show
          
hoge = Node Empty Empty -- Node Empty Empty
bar=Node(Empty) -- Affected places
barbar=bar Empty -- Node Empty Empty

main = print $barbar

In other words, it is partially applied (or is it partially applied to the data structure)?Is this the name?) Node(Empty) is considered to have the same meaning as Node Empty.

y=Node(Node(Empty), Node(Empty)) -- NG

If I try to write correctly, does it look like the following?(In the first place, () might be interpreted as a tuple at some point, so writing (,) near the constructor that doesn't receive the tables might make you think it's strange.)

Foo.hs

data Tree=Empty
          | Leaf Int
          | Node Tree Delivery Show
         
y = Node (Node Empty Empty)

main = print$y

running:

%docker run-it --rm --name bar-haskell-v "$PWD":/tmp-w/tmp haskell: 8runghc Foo.hs

Output:

 Node (Node Empty Empty)

I thought I could have two partially applied (?) nodes if I did the following, but I got an error...

data Tree=Empty
          | Leaf Int
          | Node Tree Delivery Show
         
y = Node (Node Empty)

main = print$y

() I don't know when and where to turn it on.


2022-09-30 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.