Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad idea to use [Char] instead of String in Haskell function type declaration

I have just started learning Haskell using "Learn you a Haskell for Great Good". I am currently reading "Types and Typeclasses" chapter, so my knowledge is pretty .. non-existent. I am using Sublime Text 2 with SublimeHaskell package which builds/checks file on every save.

The problem: I'm trying to make function type declaration like this:

funcName :: [Char] -> [Char]

I'm getting this warning:

Warning: Use String Found: [Char] -> [Char] Why not: String -> String

Build FAILED

Can you explain to me why is it a bad idea to use Char array instead of String or give me a link to an explanation of possible repercussions etc. I've googled and found nothing.

P.S. I'm a C# developer, I understand the difference between char array and strings in c-like languages.

like image 614
Ivan Davidov Avatar asked Nov 29 '22 08:11

Ivan Davidov


2 Answers

Somewhere in the base library you will find this definition:

type String = [Char]

which says that String and [Char] are exactly the same thing. Which of the two you choose is a documentation choice. I often define type aliases like this:

type Domain = ByteString
type UserName = Text

It's a good idea to use types for documentation.

Also as an important side note, [Char] is not the type for character arrays, but character lists. Since there are also actual array types, the distinction is important!

like image 180
ertes Avatar answered Dec 09 '22 09:12

ertes


String is nothing more than a type alias for [Char], so there is no practical between the two - it's simply a matter of readability.

like image 30
sepp2k Avatar answered Dec 09 '22 09:12

sepp2k