Re: [Haskell-cafe] n00b question: defining datatype

2009-08-06 Thread Daniel Schoepe
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


pgpx25fhhGIwn.pgp
Description: PGP signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] n00b question: defining datatype

2009-07-23 Thread Iain Barnett
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


Re: [Haskell-cafe] n00b question: defining datatype

2009-07-23 Thread Jake McArthur

Iain Barnett wrote:

data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] }


This one looks the best to me. Remember, you can just use an empty list 
if the task has no subtasks.


- Jake
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] n00b question: defining datatype

2009-07-23 Thread Max Rabkin
On Thu, Jul 23, 2009 at 8:43 PM, Iain Barnettiainsp...@gmail.com wrote:
 data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] }
 But that's not really right, because obviously, some tasks don't have
 subtasks.

The empty list is a list.

--Max
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] n00b question: defining datatype

2009-07-23 Thread Kim-Ee Yeoh

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.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] n00b question: defining datatype

2009-07-23 Thread Bulat Ziganshin
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,
 Bulatmailto:bulat.zigans...@gmail.com

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] n00b question: defining datatype

2009-07-23 Thread Thomas DuBuisson
 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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] n00b question: defining datatype

2009-07-23 Thread Jake McArthur

Actually, how about this?

import Data.Tree
newtype Task = Task (Tree (String, Bool))

Now you already have that tree structure you wanted.

- Jake
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] n00b question: defining datatype

2009-07-23 Thread Iain Barnett
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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] n00b question: defining datatype

2009-07-23 Thread Anton van Straaten

Kim-Ee Yeoh wrote:

Consider

data Task = Task { title :: String, completed :: Bool, subtasks :: Maybe
[Task] }


Note that unless you have some meaning in mind for the difference 
between a subtask value of Nothing vs. (Just []), the Maybe is redundant.


Anton

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe