[Haskell-beginners] String type hello-o-matic...

David McBride toad3k at gmail.com
Fri Mar 9 17:56:31 CET 2012


Oh, and one other thing.  There is an OverloadedStrings extension.
You can enable it with ghci -XOverloadedStrings, or put a language
pragma at the top of your source file.  This allows you to just type
strings and the compiler will infer them as Istring a => a, where all
of the above types are instances of it.  This means you can type
Data.Bytestring.PutStrLn "hello world" and it will convert the string
into bytestring when you compile.

On Fri, Mar 9, 2012 at 11:47 AM, David McBride <toad3k at gmail.com> wrote:
> It seems a little confusing at first, but it's not so bad once you
> learn a few things.
>
> For your initial question, if you import Data.ByteString, you'll end
> up with Word8 in all of the functions, including pack.  Just import
> Data.ByteString.Char8.  It is just like the other import, but all the
> functions use Chars instead of Word8's.
>
> There are only three important data types.
>
> String, which is a list of char, simple, slow, memory inefficient, but
> easy to work with.
> ByteString which is just for binary data.  Each index is 8 bits, and
> it is efficient for memory and it is one big block of data.
> Text is almost like bytestring, except that it is designed with text
> encoding in mind.  It is like a middle ground between bytestring and
> string, with very good performance, but without throwing away
> important info about characters.
>
> Both ByteString and Text have variants, strict and lazy.  If you look
> at what they are actually composed of, you'll see that all it is, is
> that instead of having one big block, the lazy variant is like a
> linked list of blocks.  That way, you can have it in memory, replace
> some text within it, append to it, etc., without having to reallocate
> the entire thing every single time you change a character.
>
> To get strings into Text, there are libraries on hackage that involve
> encoding and decoding things.  Once you get the hang of it, the only
> other thing you might need to know is how builders work.  All that
> does is try to get the chunks of the lazy variants to have uniform
> size, more or less.  But that is for a future date, when you are
> writing something that needs to be super duper efficient.
>
>
> On Fri, Mar 9, 2012 at 10:01 AM, Emacs The Viking <sean at objitsu.com> wrote:
>> Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaagh!
>>
>> Ok, that said, now I can explain my self a little more clearly.
>>
>> I am almost to the point of exasperation on trying to know when and why and
>> how to convert between String, ByteString, ByteString.Laz, Char8 etc. For
>> heavens sake how many bloody string types can you need? LOL
>>
>> I am trying to use Data.Digest.Pure.SHA but it takes a ByteString in the
>> "sha1" function but from the command prompt of ghci I cannot seem to work
>> out how to do it, I just get the usual daunting messages.
>>
>>     sha1 :: ByteString -> Digest SHA1State
>>
>> I have a String, but the pack function wants a [Word8] to create a digest
>> value.
>>
>>     pack :: [Word8] -> ByteString
>>
>> IIUC, String is just [Char] and that Char is in fact a Unicode character and
>> therefore NOT "8 bit" as such which is why the Char8 variants exist. I
>> understand the difference between lazy and strict and why one is different
>> from the other. But... it's little things like the foldr' function NOT being
>> present in one library but present in the other, presumably a lazy
>> implementation doesn't need a strict function or something like that. Who
>> knows. I don't!
>>
>> I am sure to "seasoned" haskellers this is all elementary and I would love
>> to eventually become a seasoned developer of it too but at times like this I
>> want to scream in utter confusion as to how many different string types
>> there are and the sometimes almost unnotcable differences between them. And
>> trying to convert between them to actually get on and write useful working
>> code.
>>
>> When do I use strict? When do I use lazy? When do I use the Char8 variant?
>> And so the questions continue with barely any "beginner" level answers to be
>> found. I'd write them myself but I don't know the answers yet!
>>
>> Speaking as a non-academic, non-mathemetician, but as a developer with 25+
>> years of using lots of languages inclusing Erlang, LISP and Scala, sometimes
>> Haskell is very very hard to perservere with because everybody just seems to
>> love using words and phrases like "mono-morphism", "monadic transformers"
>> etc. just to show off how well they understand Haskell. That's fine for
>> those in the club but those on the outside looking in would also like to be
>> able to "get in" as well and be able to write serious looking blog posts
>> about how damned funky haskell is.
>>
>> And it is... but not if you can't understand it! is there some kind of Free
>> Masonry involved ;)
>>
>> Thanks again,
>>
>> Sean... now where's my RWH book again...
>>
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>



More information about the Beginners mailing list