0

I'm having a problem handling my nested data types in my functions. Below is a simpler example of the issue I'm having and I'm not sure how to compute these functions properly. Any help will be much appreciated!

> data Ab               = Add Ab Bc | Bc
>                           deriving (Show, Eq) 

> data Bc               = Sub Bc Cd | Cd
>                           deriving (Show, Eq)

> data Cd               = Val Int 
>                           deriving (Show, Eq)  

> aDealer               :: Ab -> Int
> aDealer (Add a b)     = (aDealer a) + (bDealer b)
> aDealer b             = bDealer b         -- WHAT TO PUT HERE

> bDealer               :: Bc -> Int
> bDealer (Sub b c)     =  (bDealer b) + (cDealer c)
> bDealer c             = cDealer c         -- WHAT TO PUT HERE 

> cDealer               :: Cd -> Int
> cDealer (Val c)       = c

The error this code gives me is miss matched types (e.g. in aDealer b = bDealer b I get miss-matched types where bDealer can't take in Ab) I understand why I get this error but am un sure how to work round it :/

4
  • Bc :: Ab is not a value of type Bc so it can't be passed to bDealer. What would bDealer do with it? You are mixing up value namespace and type namespace it seems like. Commented Mar 12, 2015 at 15:05
  • @user2407038 Yeah I know that, that's kind of the point of the question :D I think the above Haskell makes quite logical sense about what I'm trying to achieve but ultimately it will never work, so I'm kind of looking for a work around Commented Mar 12, 2015 at 15:54
  • Perhaps you want ... | Bc Bc? Then you can write aDealer (Bc b) = bDealer b. If you can't write the function you want over the datatype you have, that is usually an indicator that your datatype is wrong. Commented Mar 12, 2015 at 21:37
  • @user2407038 O I know that's a possibility and actually it looks like that's what I'm going todo, it's just annoying as this is the data structure I'm using as an Intermediate Representation of Expressions in a compiler, so its going to get a bit annoying to write test cases! Commented Mar 12, 2015 at 22:04

1 Answer 1

1
data Ab               = Add Ab Bc | Bc

This creates two constructors: one is called Add and takes an Ab and a Bc parameter. The other is called Bc and takes no parameters.

You need to give the second constructor a proper name. Then you can use that in pattern matching.

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

3 Comments

But given that this is a valid data type is there not another way to write the function aDealer not necessarily using pattern matching but still getting the same outcome, for no other reason than once you start having 5-6 nested data types it will get messy
@LJackso You might want to consider redesigning your data structures.
I would but this is eventually used as part of a compiler and is my Intermediate representation for Expressions, so it i s really the best way to do it :/

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.