Quantcast

ByteString in patterns

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

ByteString in patterns

Manlio Perillo-3
Hi.

Using normal String type I can define a pattern like:

 > let foo "baz" = 777
 > foo "baz"
777


But if I want to use ByteString, what should I do?
This seems impossible, since ByteString data constructor is not available.


Thanks  Manlio Perillo
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Bryan O'Sullivan
On Tue, Mar 10, 2009 at 3:28 PM, Manlio Perillo <[hidden email]> wrote:
This seems impossible, since ByteString data constructor is not available.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Bulat Ziganshin-2
In reply to this post by Manlio Perillo-3
Hello Manlio,

Wednesday, March 11, 2009, 1:28:13 AM, you wrote:

> Using normal String type I can define a pattern like:

> But if I want to use ByteString, what should I do?
> This seems impossible, since ByteString data constructor is not available.

for numeric types, it works via Num instances. we have now IsString
type, may be it provides the same feature? i.e., are you tried? :)

--
Best regards,
 Bulat                            mailto:[hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Don Stewart-2
In reply to this post by Manlio Perillo-3
manlio_perillo:

> Hi.
>
> Using normal String type I can define a pattern like:
>
> > let foo "baz" = 777
> > foo "baz"
> 777
>
>
> But if I want to use ByteString, what should I do?
> This seems impossible, since ByteString data constructor is not available.

-XOverloadedStrings

e.g.

    {-# LANGUAGE OverloadedStrings #-}

    import qualified Data.ByteString.Char8 as C

    isMatch :: C.ByteString -> Bool
    isMatch "match" = True
    isMatch _       = False

    main = print . map isMatch . C.lines =<< C.getContents
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Manlio Perillo-3
Don Stewart ha scritto:

> manlio_perillo:
>> Hi.
>>
>> Using normal String type I can define a pattern like:
>>
>>> let foo "baz" = 777
>>> foo "baz"
>> 777
>>
>>
>> But if I want to use ByteString, what should I do?
>> This seems impossible, since ByteString data constructor is not available.
>
> -XOverloadedStrings
>

Perfect, thanks.

Is this supported by other Haskell implementations, or planned for Haskell'?


Manlio
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Don Stewart-2
manlio_perillo:

> Don Stewart ha scritto:
>> manlio_perillo:
>>> Hi.
>>>
>>> Using normal String type I can define a pattern like:
>>>
>>>> let foo "baz" = 777
>>>> foo "baz"
>>> 777
>>>
>>> But if I want to use ByteString, what should I do?
>>> This seems impossible, since ByteString data constructor is not available.
>>
>> -XOverloadedStrings
>
> Perfect, thanks.
>
> Is this supported by other Haskell implementations, or planned for Haskell'?

Not as far as I know. It was added to GHC just over 2 years ago,

    http://article.gmane.org/gmane.comp.lang.haskell.cvs.all/31022

and isn't terribly widely used. Probably ByteString literals are the
current main use (along with interesting EDSL applications, which was
the original motivation afaik).

-- Don
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Manlio Perillo-3
Don Stewart ha scritto:

> [...]
>>> -XOverloadedStrings
>> Perfect, thanks.
>>
>> Is this supported by other Haskell implementations, or planned for Haskell'?
>
> Not as far as I know. It was added to GHC just over 2 years ago,
>
>     http://article.gmane.org/gmane.comp.lang.haskell.cvs.all/31022
>
> and isn't terribly widely used. Probably ByteString literals are the
> current main use (along with interesting EDSL applications, which was
> the original motivation afaik).
>

Ok, thanks.

In my case it just makes the code nicer; it is not a critical feature.


Manlio
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Manlio Perillo-3
In reply to this post by Don Stewart-2
Don Stewart ha scritto:

> [...]
>     {-# LANGUAGE OverloadedStrings #-}
>
>     import qualified Data.ByteString.Char8 as C
>
>     isMatch :: C.ByteString -> Bool
>     isMatch "match" = True
>     isMatch _       = False
>
>     main = print . map isMatch . C.lines =<< C.getContents
>

What is the reason why instance declarations for IsString class are not
defined for available ByteStrings?

I need to define it by myself.



Thanks  Manlio Perillo
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Don Stewart-2
manlio_perillo:

> Don Stewart ha scritto:
>> [...]
>>     {-# LANGUAGE OverloadedStrings #-}
>>
>>     import qualified Data.ByteString.Char8 as C
>>
>>     isMatch :: C.ByteString -> Bool
>>     isMatch "match" = True
>>     isMatch _       = False
>>
>>     main = print . map isMatch . C.lines =<< C.getContents
>>
>
> What is the reason why instance declarations for IsString class are not  
> defined for available ByteStrings?
>
> I need to define it by myself.

They're exported from Data.ByteString.Char8
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Manlio Perillo-3
Don Stewart ha scritto:

> manlio_perillo:
>> Don Stewart ha scritto:
>>> [...]
>>>     {-# LANGUAGE OverloadedStrings #-}
>>>
>>>     import qualified Data.ByteString.Char8 as C
>>>
>>>     isMatch :: C.ByteString -> Bool
>>>     isMatch "match" = True
>>>     isMatch _       = False
>>>
>>>     main = print . map isMatch . C.lines =<< C.getContents
>>>
>> What is the reason why instance declarations for IsString class are not  
>> defined for available ByteStrings?
>>
>> I need to define it by myself.
>
> They're exported from Data.ByteString.Char8
>

Then there is something I'm missing.
Your code does not compile.



Thanks  Manlio Perillo

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Don Stewart-2
manlio_perillo:

> Don Stewart ha scritto:
>> manlio_perillo:
>>> Don Stewart ha scritto:
>>>> [...]
>>>>     {-# LANGUAGE OverloadedStrings #-}
>>>>
>>>>     import qualified Data.ByteString.Char8 as C
>>>>
>>>>     isMatch :: C.ByteString -> Bool
>>>>     isMatch "match" = True
>>>>     isMatch _       = False
>>>>
>>>>     main = print . map isMatch . C.lines =<< C.getContents
>>>>
>>> What is the reason why instance declarations for IsString class are
>>> not  defined for available ByteStrings?
>>>
>>> I need to define it by myself.
>>
>> They're exported from Data.ByteString.Char8
>>
>
> Then there is something I'm missing.
> Your code does not compile.

Sure it does:

    $ ghci A.hs  
    GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
    Loading package ghc-prim ... linking ... done.
    Loading package integer ... linking ... done.
    Loading package base ... linking ... done.
    [1 of 1] Compiling Main             ( A.hs, interpreted )
    Ok, modules loaded: Main.

    *Main> :t main
    main :: IO ()

You should give any error message, and the steps you took that lead to
the error when making a bug report.

-- Don
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Daniel Fischer-4
In reply to this post by Manlio Perillo-3
Am Mittwoch, 11. März 2009 17:09 schrieb Manlio Perillo:

> Don Stewart ha scritto:
> > manlio_perillo:
> >> Don Stewart ha scritto:
> >>> [...]
> >>>     {-# LANGUAGE OverloadedStrings #-}
> >>>
> >>>     import qualified Data.ByteString.Char8 as C
> >>>
> >>>     isMatch :: C.ByteString -> Bool
> >>>     isMatch "match" = True
> >>>     isMatch _       = False
> >>>
> >>>     main = print . map isMatch . C.lines =<< C.getContents
> >>
> >> What is the reason why instance declarations for IsString class are not
> >> defined for available ByteStrings?
> >>
> >> I need to define it by myself.
> >
> > They're exported from Data.ByteString.Char8
>
> Then there is something I'm missing.

A recent enough bytestring package.
Compiles and works with 0.9.1.4

> Your code does not compile.
>
>
>
> Thanks  Manlio Perillo
>

Cheers,
Daniel
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ByteString in patterns

Manlio Perillo-3
In reply to this post by Don Stewart-2
Don Stewart ha scritto:
> [...]
>> Then there is something I'm missing.
>> Your code does not compile.
>
> Sure it does:
>

As Daniel suggested, I'm using an old bytestring version that came with
Debian Etch (GHC 6.8.2).



Thanks  Manlio
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Loading...