Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Map on Nested Lists (Felipe Lessa)
   2.  private types (Ashish Agarwal)
   3. Re:  Encoding strings (legajid)
   4. Re:  private types (Stephen Tetley)
   5. Re:  private types (Ashish Agarwal)
   6. Re:  Encoding strings (aditya siram)
   7. Re:  private types (matthew coolbeth)


----------------------------------------------------------------------

Message: 1
Date: Thu, 8 Apr 2010 14:36:53 -0300
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Map on Nested Lists
To: Tim Sears <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Thu, Apr 8, 2010 at 12:58 PM, Tim Sears <[email protected]> wrote:
> If you don't mind choosing a different data type (always a good question to
> ask yourself in haskell) you could simply use your own nested lists....
> data Nested a  = One a | Nest [Nested a]
>                  deriving Show

Or, if you don't want to allow different "nested-ness":

  data Nested a = One a | Nest (Nested [a])

Examples of data types:

  One a :: Nested a
  Nest (One [a,b,c]) :: Nested a
  Nest (Nest (One [[a,b],[c,d]])) :: Nested a

  fromList :: [a] -> Nested a
  fromList = Nest . One

  fromList2 :: [[a]] -> Nested a
  fromList2 = Nest . Nest . One

The functor instance:

  instance Functor Nested where
    fmap f (One a) = One (f a)
    fmap f (Nest xs) = Nest (fmap (fmap f) xs)

Not tested, use at your own risk ;).

Cheers,

-- 
Felipe.


------------------------------

Message: 2
Date: Thu, 8 Apr 2010 15:11:23 -0400
From: Ashish Agarwal <[email protected]>
Subject: [Haskell-beginners] private types
To: Haskell-beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Is there a Haskell analogue to OCaml's private types? In OCaml, you can do
this (converting to pseudo-Haskell syntax):

type T = private A Int

Then, A cannot be used directly to construct values of type T.
A 3
Error: Cannot create values of the private type T

You must provide another constructor, makeT, which takes an Int and returns
a T. This allows you to do additional checks on the Int argument. However,
you can still deconstruct values of type T.

f (A x) = x + 2

This works even when f is defined in a different module. The benefit is that
you can restrict the allowed values of T, but still have the convenience of
using existing operations on these values once constructed.

The solution that comes to mind is to make T abstract and an instance of
some appropriate class, but there is no class that lets you pattern match on
arbitrary variant types.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100408/2ef22442/attachment-0001.html

------------------------------

Message: 3
Date: Thu, 08 Apr 2010 21:26:56 +0200
From: legajid <[email protected]>
Subject: Re: [Haskell-beginners] Encoding strings
To: aditya siram <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi,
i've  found another solution : instead of putStr (show text), i just 
write putStr text.

As a remaining problem, i don't how to install cabal in order to install 
the desired module.

Thanks,
Didier.


aditya siram a écrit :
> This is because of Unicode strings. You need to import
> "Codec.Binary.UTF8.String" and use:
> decodeString :: String -> String
> and
> encodeString :: String -> String
>
> hth,
> deech
>
>
> On 4/7/10, legajid <[email protected]> wrote:
>   
>> Hi,
>>
>> i experiment problems writing files.
>> Reading an access 97 database and displaying data in a wxhaskell grid,
>> everything is correct.
>> When i write data in a text file, some characters are translated : "é"
>> becomes \233.
>>
>> How to correct this ?
>> I looked for hSetEncoding but didn't find it in System.IO, nor in
>> GHC.IO.Handle that doesn't exist on my system.
>> I tried hSetBinaryMode, without success.
>>
>> If this helps, I run ghc 6.10.4 on windows XP.
>>
>> Thanks for an idea,
>> Didier.
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>     
>
>   


------------------------------

Message: 4
Date: Thu, 8 Apr 2010 20:42:22 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] private types
To: Ashish Agarwal <[email protected]>
Cc: Haskell-beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi

I don't think there is a direct equivalence. You can make T an opaque
type using a module where you do not export the constructor. However,
you can't then pattern match on the type so you have to supply a
destructor - here called unT. Any module that imports XYZ only sees
the type T, the 'handmade' constructor makeT and the destructor unT:



module XYZ
  (
    T,         -- this does not export the constructor
    unT
    makeT

  ) where

data T = PrivateT Int   deriving (Eq,Show)

unT :: T -> Int
unT (PrivateT i) = i

-- Add any checking you want here...
makeT :: Int -> T
makeT i = PrivateT i


------------------------------

Message: 5
Date: Thu, 8 Apr 2010 15:54:12 -0400
From: Ashish Agarwal <[email protected]>
Subject: Re: [Haskell-beginners] private types
To: Stephen Tetley <[email protected]>
Cc: Haskell-beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

This makes T fully abstract, which is what I'm trying to avoid. Private
types provide an intermediate level of abstraction. The real example I have
involves defining a type with numerous constructors (or a record with
numerous fields), and I'd like to avoid writing all the corresponding
destructors. Even if I did do this, it burdens clients of the module by
requiring them to use the alternative destructors.

I think the feature I'm looking for is "guarded constructors", but to my
knowledge there is no such thing.


On Thu, Apr 8, 2010 at 3:42 PM, Stephen Tetley <[email protected]>wrote:

> Hi
>
> I don't think there is a direct equivalence. You can make T an opaque
> type using a module where you do not export the constructor. However,
> you can't then pattern match on the type so you have to supply a
> destructor - here called unT. Any module that imports XYZ only sees
> the type T, the 'handmade' constructor makeT and the destructor unT:
>
>
>
> module XYZ
>  (
>    T,         -- this does not export the constructor
>    unT
>    makeT
>
>  ) where
>
> data T = PrivateT Int   deriving (Eq,Show)
>
> unT :: T -> Int
> unT (PrivateT i) = i
>
> -- Add any checking you want here...
> makeT :: Int -> T
> makeT i = PrivateT i
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100408/7b3e8161/attachment-0001.html

------------------------------

Message: 6
Date: Thu, 8 Apr 2010 14:57:18 -0500
From: aditya siram <[email protected]>
Subject: Re: [Haskell-beginners] Encoding strings
To: legajid <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

To install it (or any other module) I usually look up the module on
Hayoo [1]. It will tell you the source package. In this case it is
'utf8-string' so just do:
> cabal install utf8-string.

-deech

[1] http://holumbus.fh-wedel.de/hayoo/hayoo.html

On 4/8/10, legajid <[email protected]> wrote:
> Hi,
> i've  found another solution : instead of putStr (show text), i just
> write putStr text.
>
> As a remaining problem, i don't how to install cabal in order to install
> the desired module.
>
> Thanks,
> Didier.
>
>
> aditya siram a écrit :
>> This is because of Unicode strings. You need to import
>> "Codec.Binary.UTF8.String" and use:
>> decodeString :: String -> String
>> and
>> encodeString :: String -> String
>>
>> hth,
>> deech
>>
>>
>> On 4/7/10, legajid <[email protected]> wrote:
>>
>>> Hi,
>>>
>>> i experiment problems writing files.
>>> Reading an access 97 database and displaying data in a wxhaskell grid,
>>> everything is correct.
>>> When i write data in a text file, some characters are translated : "é"
>>> becomes \233.
>>>
>>> How to correct this ?
>>> I looked for hSetEncoding but didn't find it in System.IO, nor in
>>> GHC.IO.Handle that doesn't exist on my system.
>>> I tried hSetBinaryMode, without success.
>>>
>>> If this helps, I run ghc 6.10.4 on windows XP.
>>>
>>> Thanks for an idea,
>>> Didier.
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>>
>>
>>
>


------------------------------

Message: 7
Date: Thu, 8 Apr 2010 16:06:33 -0400
From: matthew coolbeth <[email protected]>
Subject: Re: [Haskell-beginners] private types
To: Ashish Agarwal <[email protected]>
Cc: Haskell-beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

A variant on Stephen's solution is to use Haskell's record syntax, which
will generate the "destructors" implicitly.  This is no easier on your
users, but much easier for you to write, depending on the structure of your
type.

On Thu, Apr 8, 2010 at 15:54, Ashish Agarwal <[email protected]> wrote:

> This makes T fully abstract, which is what I'm trying to avoid. Private
> types provide an intermediate level of abstraction. The real example I have
> involves defining a type with numerous constructors (or a record with
> numerous fields), and I'd like to avoid writing all the corresponding
> destructors. Even if I did do this, it burdens clients of the module by
> requiring them to use the alternative destructors.
>
> I think the feature I'm looking for is "guarded constructors", but to my
> knowledge there is no such thing.
>
>
> On Thu, Apr 8, 2010 at 3:42 PM, Stephen Tetley 
> <[email protected]>wrote:
>
>> Hi
>>
>> I don't think there is a direct equivalence. You can make T an opaque
>> type using a module where you do not export the constructor. However,
>> you can't then pattern match on the type so you have to supply a
>> destructor - here called unT. Any module that imports XYZ only sees
>> the type T, the 'handmade' constructor makeT and the destructor unT:
>>
>>
>>
>> module XYZ
>>  (
>>    T,         -- this does not export the constructor
>>    unT
>>    makeT
>>
>>  ) where
>>
>> data T = PrivateT Int   deriving (Eq,Show)
>>
>> unT :: T -> Int
>> unT (PrivateT i) = i
>>
>> -- Add any checking you want here...
>> makeT :: Int -> T
>> makeT i = PrivateT i
>>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>


-- 
mac
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100408/22fdd4e0/attachment.html

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 22, Issue 11
*****************************************

Reply via email to