
Hi, I'm trying to get my head around datatypes, and wondering how I might define a simple "Task" datatype in Haskell. data Task = Task { title :: String, completed :: Bool } Ok, that's straightforward, but sometimes tasks become a list of tasks themselves data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] } But that's not really right, because obviously, some tasks don't have subtasks. So I try this: data Task = Task { title :: String, completed :: Bool } | TaskWithSubtasks { title :: String, completed :: Bool, subtasks :: [Task] } It's a bit more accurate, but it's repeating things, which is ok with a simple type. Could anyone suggest a better way to define this? If I was using C#, which I'm far more familiar with, I could overload the constructor and refer to the smaller constructor. Is there a way to do that in Haskell, or am I still thinking too OOP? Iain

Consider data Task = Task { title :: String, completed :: Bool, subtasks :: Maybe [Task] } Iain Barnett wrote:
Hi, I'm trying to get my head around datatypes, and wondering how I might define a simple "Task" datatype in Haskell. data Task = Task { title :: String, completed :: Bool } Ok, that's straightforward, but sometimes tasks become a list of tasks themselves data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] } But that's not really right, because obviously, some tasks don't have subtasks. So I try this: data Task = Task { title :: String, completed :: Bool } | TaskWithSubtasks { title :: String, completed :: Bool, subtasks :: [Task] } It's a bit more accurate, but it's repeating things, which is ok with a simple type. Could anyone suggest a better way to define this? If I was using C#, which I'm far more familiar with, I could overload the constructor and refer to the smaller constructor. Is there a way to do that in Haskell, or am I still thinking too OOP? Iain
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/n00b-question%3A-defining-datatype-tp24631976p24632019... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Hello Iain, Thursday, July 23, 2009, 10:43:02 PM, you wrote:
data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] } But that's not really right, because obviously, some tasks don't have subtasks.
don't see a problem - subtasks list may be empty -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] } But that's not really right, because obviously, some tasks don't have
As Jake said - that's fine and you can still pattern match on the null ([]) taskes when looking for tasks without subtaskes.
data Task = Task { title :: String, completed :: Bool } | TaskWithSubtasks { title :: String, completed :: Bool, subtasks :: [Task] } It's a bit more accurate, but it's repeating things, which is ok with a simple type. Could anyone suggest a better way to define this? If I was using C#, which I'm far more familiar with, I could overload the constructor and refer to the smaller constructor.
So do you want: data Job = Job {title :: String, completed :: Bool } data Task = Task Job | MasterTask String [Job] Tom

Actually, how about this? import Data.Tree newtype Task = Task (Tree (String, Bool)) Now you already have that tree structure you wanted. - Jake

Ok, thanks to everyone, that's certainly answered my question and given me some more avenues to pursue. I can see now that because I can pattern match against the empty list it's not really a problem to have it there. I didn't realise I could use Maybe in the constructor because it's a monad, but that's good because I was wondering about the best way to make a nullable value. That Data.Tree module looks interesting too! It does seem to be a naturally recursive type, but I'm still trying to become easy with that sort of thought :) Thanks for all the help, it's nice to get a bit of feedback when still getting used to things. Iain

On Thu, Jul 23, 2009 at 08:17:34PM +0100, Iain Barnett wrote:
[..] against the empty list it's not really a problem to have it there. I didn't realise I could use Maybe in the constructor because it's a monad, but that's good because I was wondering about the best way to make a nullable value.
Actually, this has nothing to do with Maybe being a monad. The reason you can do this is because "Maybe" itself is not a type, but a (unary) type constructor(It has kind * -> *), so you need to apply it to another type. Doing something like "test :: Maybe" would be an error.
That Data.Tree module looks interesting too! It does seem to be a naturally recursive type, but I'm still trying to become easy with that sort of thought :) [..]
A list is also recursively defined, so it is not really more difficult to use a Tree instead. E.g. one could define a list type like this: data List a = Nil | Cons a (List a) - Daniel
participants (8)
-
Anton van Straaten
-
Bulat Ziganshin
-
Daniel Schoepe
-
Iain Barnett
-
Jake McArthur
-
Kim-Ee Yeoh
-
Max Rabkin
-
Thomas DuBuisson