[Haskell-cafe] n00b question: defining datatype
Kim-Ee Yeoh
a.biurvOir4 at asuhan.com
Thu Jul 23 14:47:05 EDT 2009
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 at 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.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
More information about the Haskell-Cafe
mailing list