سؤال

I do not understand why this does not work. My data type is as follows:

data IndexedTree a = Leaf [a] | Node [(IndexedTree a, a)]

first (x, _ ) = x
test :: IndexedTree a -> Bool
test (Leaf x) = True
test (Node (x:xs)) = test first(x)

yields to

Could not match expected type IndexedTree a0
with actual type (t0,t1)-> t0

in the last LOC. I dont get why this is happening and how to avoid this.

هل كانت مفيدة؟

المحلول

Your definition of test should be:

test :: IndexedTree a -> Bool
test (Leaf x) = True
test (Node (x:xs)) = test (first x)

first (x) is the same as first x, and since function application associates to the left

test first (x)

is parsed as

(test first) x

test expects an IndexedTree a argument but first has type (a, b) -> a, hence the error.

You should also handle the case where the node list is empty.

نصائح أخرى

first(x) is the same as first (x), which is the same as first x. So test first(x) is just test first x, and test expects only one argument.

If you want to evaluate first x first:

test (Node (x:xs)) = test $ first x
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top