data Tree=Empty
| Leaf Int
| Node Tree Tree
v=Node(Leaf100) (Node(Leaf200)(Leaf300)) -- OK
w = Empty -- OK
x = Node (Empty) -- OK
y=Node(Node(Empty), Node(Empty)) -- NG
z = Node (Empty, Empty) -- NG
When I wrote the above code,
y=Node(Node(Empty), Node(Empty)) -- NG
The error is
<playground>:8:9:error:
• Couldn't match expected type 'Tree'
with actual type '(Tree-> Tree, Tree-> Tree)'
• In the first argument of 'Node', namely
'(Node(Empty), Node(Empty))'
In the expression: Node (Node (Empty), Node (Empty))
In an equation for 'y': y = Node (Node(Empty), Node(Empty))
is now
z=Node(Empty, Empty) -- NG
The error is
<playground>:9:9:error:
• Couldn't match expected type 'Tree'
with actual type '(Tree, Tree)'
• In the first argument of 'Node', namely' (Empty, Empty)'
In the expression: Node (Empty, Empty)
In anequation for 'z': z = Node (Empty, Empty)
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
-- 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.
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.
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.
© 2024 OneMinuteCode. All rights reserved.