0

I have 3 functions, the first one is a

toList :: Tree -> [Integer]

the second is

sumTree :: Tree -> Maybe Integer

which sums a the elements in a tree, filtering for some edge cases. My main function is

treeSum :: Tree -> Maybe Integer

which I want to call the toList on the input to Tree then call sumTree on the output of the previous call. I don't know how to structure it together elegantly. What I did was

treeSum = sumTree (toList x)

but I am getting x not in scope.

3
  • Because x is not defined. You probably want treeSum to take x as an argument, which in Haskell is written treeSum x = .... Exercise: what will the type of x be? Commented Mar 7, 2015 at 6:16
  • Ah oops. So I tried, treeSum x = sumTree (toList x), isn't working too Commented Mar 7, 2015 at 6:18
  • The type of x will be a Maybe Integer, my desired output? Commented Mar 7, 2015 at 6:21

1 Answer 1

1

What Haskell is telling you by saying Not in scope is that x is defined nowhere else.

You should write something like:

treeSum x = sumTree (toList x)

The problem you will then face is a type error. toList takes a Tree and returns a List of Integer but sumTree is waiting for a Tree, not a list of Integer.

To be honest, What you’re trying to do is not really clear.

Note: you should avoid having both sumTree and treeSum defined in your code because you will loose yourself and everyone who will try and read your code.

Sign up to request clarification or add additional context in comments.

3 Comments

stackoverflow.com/questions/28894252/… trying to do this question really.
With all the answers you get for that previous question, it's even less clear what you miss to achieve your goal. Have you seen that hackage.haskell.org/package/base-4.7.0.2/docs/… gives an example that nearly does what you want to do ?
Could you give examples of trees you will be working on and what you expect the functions to return ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.