[Haskell-begin] data declarations

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Haskell-begin] data declarations

Roger Myers
hi

Not sure if I'm on the right track here, so as a beginner I thought I'd ask on
this list.

I want to declare a type 'All' that consists of the of the types 'Tinteger,
Tbool, Tstring, Tarray' but I would also like to make a relationship that
Tinteger and Tbool are also the type 'Simple' and that 'Tstring' and 'Tarray'
are 'Composite' and that Mixed is then either Simpletype or Composite.

So I can then write a function that takes a type of All and produces a list of
Mixed.

data All = Tinteger Int
              | Tbool Bool
              | Tstring String
              | Tarray [Alltypes]

data Simple = Tinteger
            | Tbool
data Composite = Tstring
               | Tarray

data Mixed = Simple
           | Composite

k :: All -> [Mixed]
k a = case a of
        Simple s -> [Simple s]
        Composite c -> [Composite c]


But this isn't valid Haskell code because I can't have multiple declarations of
the same types, is there a way to specify a relationship, if so can someone
point me in the right direction?

Roger

Reply | Threaded
Open this post in threaded view
|

[Haskell-begin] data declarations

Brandon S Allbery KF8NH

On 2008 Jul 22, at 23:01, Roger Myers wrote:

> Not sure if I'm on the right track here, so as a beginner I thought  
> I'd ask on
> this list.
>
> I want to declare a type 'All' that consists of the of the types  
> 'Tinteger,
> Tbool, Tstring, Tarray' but I would also like to make a relationship  
> that
> Tinteger and Tbool are also the type 'Simple' and that 'Tstring' and  
> 'Tarray'
> are 'Composite' and that Mixed is then either Simpletype or Composite.
>
> So I can then write a function that takes a type of All and produces  
> a list of
> Mixed.


Any particular reason that All can't be represented by Mixed?  That  
would be my first cut at it (assuming I didn't need advanced type  
hackery for some reason).

--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [hidden email]
system administrator [openafs,heimdal,too many hats] [hidden email]
electrical and computer engineering, carnegie mellon university    KF8NH


Reply | Threaded
Open this post in threaded view
|

[Haskell-begin] data declarations

Jeff Zaroyko
Brandon S. Allbery KF8NH <allbery <at> ece.cmu.edu> writes:

>
>
> On 2008 Jul 22, at 23:01, Roger Myers wrote:
>
> > Not sure if I'm on the right track here, so as a beginner I thought  
> > I'd ask on
> > this list.
> >
> > I want to declare a type 'All' that consists of the of the types  
> > 'Tinteger,
> > Tbool, Tstring, Tarray' but I would also like to make a relationship  
> > that
> > Tinteger and Tbool are also the type 'Simple' and that 'Tstring' and  
> > 'Tarray'
> > are 'Composite' and that Mixed is then either Simpletype or Composite.
> >
> > So I can then write a function that takes a type of All and produces  
> > a list of
> > Mixed.
>
> Any particular reason that All can't be represented by Mixed?  That  
> would be my first cut at it (assuming I didn't need advanced type  
> hackery for some reason).
>

Hmm... ah, bad thinking on my part.  I think this serves me a lot better:


data Simple = Tinteger Int
            | Tbool Bool
data Composite = Tstring String
               | Tarray [All]

data All = Simple | Composite

k :: All -> [All]
k a = case a of
        Simple -> [a]
        Composite -> [a]


Thanks :)

Reply | Threaded
Open this post in threaded view
|

[Haskell-begin] data declarations

Bas van Dijk-2
On Wed, Jul 23, 2008 at 5:42 AM, Jeff Zaroyko <[hidden email]> wrote:
> data Simple = Tinteger Int
>            | Tbool Bool
> data Composite = Tstring String
>               | Tarray [All]
>
> data All = Simple | Composite

Don't you mean:

data All = Simple Simple | Composite Composite

So the 'All' type contains the 'Simple' and 'Composite' types. In case
you find it confusing that the constructor names are the same as the
names of the types you can write something like:

data All = S Simple | C Composite

I don't know what your 'k' should do. Because your function...

> k :: All -> [All]
> k a = case a of
>        Simple -> [a]
>        Composite -> [a]

...can be simplified to: 'k a = [a]'  (if you disregard the strictness
properties)

which isn't very useful. So I guess you mean something different.

regards,

Bas