1

Greetings, I am new to Haskell and I've gotten stuck in defining a datatype for an assignment.

I need to create the "Strategy" type, it's basically a string with 1-6 characters each representing a numeric value, and I must represent values greater than 9 as a letter (up to 35 total different values), I tried defining an auxiliary type representing each possible value and using that to create my type, but my code isn't working and I have ran out of ideas. This is the definition I have been trying:

data Value = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'A' | 'B' |
 'C' | 'D' | 'E' | 'F' | 'G' | 'I' |'J' | 'K' | 'L' | 'M' |  'N' | 'O' | 'P' |
 'Q' |'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'Y' | 'X' | 'Z'

data Strategy = Value | Value:Value | Value:Value:Value |
Value:Value:Value:Value | Value:Value:Value:Value:Value |             
Value:Value:Value:Value:Value:Value

The Value type isn't accepting the numbers, and the Strategy type "sort of" works up to the second constructor after which it goes bust. Thanks for your help!

4
  • That's because you have missed something fundamental about datatypes. Even though you'll soon get an answer with the correct code, you need to understand it! Datatypes aren't enumerations of possible values (well, they can be and they actually are from a more abstract point of view, but it definitely doesn't work like this). Commented Dec 1, 2010 at 17:12
  • thanks for your imput, I do need to understand this, and actually I was under the impression I could use a datatype to create an enumerated type as I would do say in java (as long as none of the constructors had any parameters. I was also aiming to be able to use "deriving (Eq, Ord, Enum)" to be able to operate on the values, since I have several functions to define that operate on the Strategy type. Commented Dec 1, 2010 at 17:16
  • Datatype can (and indeed is) used for enumerations - some constructores without parameters, as you said. Real example from the stdlib: data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode. Seems like you do know a bit about datatypes? Commented Dec 1, 2010 at 17:28
  • I've done some research and know a bit about them, but apparently not enough to suit my needs, or at least I am not being able to use them properly. Commented Dec 1, 2010 at 17:50

2 Answers 2

6

Just like your previous (deleted) question, this looks a lot like homework. It also helps if you pick a real name and not use this throw-away account.

This page tells you how to use haskell's data type declarations. What are you doing different/wrong?

  1. You use characters instead of constructors for Value. The '1' '2', etc are all characters, you need a constructor along with zero or more fields like ValueCon1 Char and ValueCon2 String

  2. You are using list constructors and what I assume is a field of Value instead of defining any new constructors for Strategy. Perhaps you want data Strategy = Strat [Value], which will accept a list of any size. While more painful you can define many many strategy constructors with a finite number of separate Value fields.

BTW - where is your course taught? It seems late in the term to be covering these basics.

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

3 Comments

Thanks for your answer, and actually I just registered on SO, this isn't a throwaway account and I didn't delete any question. I will try your suggestion of defining many strategy constructors as I want a max of 6 possible characters. This is part of a course project which is Haskell based, but I am more confident when it comes to dealing with functions and have been struggling with datatypes, can't get very far without having the correct types!
@user: With several constructors, "cons ing" (const = :) still won't work. In the declaration, you specify typed - but cons is a constructor (albeit a pretty important one one with special syntax), not a type. The type is a list, which is unbounded in size. If you want to restrict arity, use tuples ((Value, Value) is two Values and nothing else - list functions won't work on them though, which can make them unwieldy).
About the first part of your answer, I was trying to follow the example of defining a weekdays type, such as "day = Monday | Tuesday | etc" that I've seen used as an example of enumerated types, so I was trying to do something similar but where each constructor would be a character, then I tried removing the ' marks and the interpreter wouldn't let me use the numeric values as elements of the type...
1

type Strategy = [Char]

^^ how I'd actually do it.

Or, maybe

newtype Strategy = Strategy [Char]

This is less principled, as the obligation is on you to not fill it with nonsense. Using the newtype solution, you can use "smart constructors" and say, e.g.

mkStrategy :: [Char] -> Strategy
mkStrategy x
   | {- list is valid -} = Strategy x
   | otherwise = error "you gave me a bad strategy!"

in real code, you don't want to throw error, but return an option type, but that's another story...

1 Comment

That's actually what I ended up doing and it made my problem much simpler, though I'm unsure about dealing with "unwanted" values. Thanks for your answer!

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.