<div>Hi,<br></div><div></div><div>I'm trying to get my head around datatypes, and wondering how I might define a simple "Task" datatype in Haskell.</div><div></div><div>data Task = Task { title :: String, completed :: Bool }</div>
<div></div><div>Ok, that's straightforward, but sometimes tasks become a list of tasks themselves</div><div></div><div>data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] }</div><div></div><div>But that's not really right, because obviously, some tasks don't have subtasks. So I try this:</div>
<div></div><div>data Task = Task { title :: String, completed :: Bool } | TaskWithSubtasks { title :: String, completed :: Bool, subtasks :: [Task] }</div><div></div><div>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?</div>
<div></div><div>Iain</div>