Reputation: 249
I have created new type describing Binary Tree
data BinTree a = Null | Num a (BinTree a) (BinTree a)
deriving (Show)
And have created the following function:
treehandle :: BinTree a -> Bool
treehandle a = True
to check at least inputing values.
When I input value Null, program outputs result successfully, but I can't input binary tree. I try thus:
treehandle (5 (Null) (Null))
but obtain:
<interactive>:66:13:
No instance for (Num (BinTree a1 -> BinTree a2 -> BinTree a0))
(maybe you haven't applied enough arguments to a function?)
arising from the literal ‘5’
In the expression: 5
In the first argument of ‘treehandle’, namely ‘(5 (Null) (Null))’
In the expression: treehandle (5 (Null) (Null))
Why?
Upvotes: 1
Views: 137
Reputation: 249
I thank for your replies. I appreciate it. Haskell is "wild" language being mix of Lisp and Prolog. Gradually I begin to get used to it. I would share with my results. So, that's BinTree declaration:
data BinTree a = Leaf | Val a (BinTree a) (BinTree a)
deriving (Show)
It resembles on Prolog unification algorithm - I mean about definition of "reverse" as below:
reverse :: BinTree a -> BinTree a
reverse (Val a1 (a2) (a3)) = (Val a1 (reverse a3) (reverse a2))
reverse Leaf = Leaf
It works!
Upvotes: 0
Reputation: 281
I would find a different naming for the data constructors if I was you. Num
is also the name of a type class, this can be very confusing when looking at error messages.
Also deriving Show
is not correctly indented, and you forgot the data constructor in treehandle (5 (Null) (Null))
. Here is a working version.
data BinTree a = Leaf | Node a (BinTree a) (BinTree a) deriving Show
treehandle :: BinTree a -> Bool
treehandle _ = True
test = treehandle $ Node 5 Leaf Leaf
treehandle
wants a value of type BinTree a
and all you gave it was an Int
and two empty BinTree's
, it actually tried to apply the Int
with the two empty BinTree's
and failed. You have to make a Node
to get a single BinTree a
which you can pass to treehandle
Upvotes: 6
Reputation: 70277
You forgot the value constructor's name
treehandle (Num 5 (Null) (Null))
Upvotes: 11