4

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.

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
Ivan Davidov
  • 823
  • 1
  • 9
  • 24
  • 5
    `[Char]` and `String` are the same thing. I don't know what SublimeHaskell does, but I'm guessing it's running some sort of lint-type checking on your code. – Lily Ballard Jan 22 '13 at 23:49
  • 1
    From now on, I'm going to use only notepad++ and ghci in order to reduce the noise while learning. Thanks. – Ivan Davidov Jan 23 '13 at 00:21
  • 2
    As a side note, you should use `Text` for serious coding when you become more proficient with Haskell. It offers better space efficiency and performance. Lazy lists are better control structures than data structures. – Gabriella Gonzalez Jan 23 '13 at 03:16
  • Rule in the ST2 plugin may be an artifact of this: http://book.realworldhaskell.org/read/using-typeclasses.html#id607949 – Gene T Jan 26 '13 at 16:35

3 Answers3

13

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!

ertes
  • 4,430
  • 1
  • 18
  • 23
  • Thanks for additional tips – Ivan Davidov Jan 23 '13 at 00:10
  • In addition using type aliases sometimes makes it easier to refactor the code later on. When refactoring functions with a type alias you only have to refactor the implementation while the type signature stays the same. – Laar Jan 23 '13 at 14:08
7

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.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
4

You seem to be running HLint on your code automatically, and treating any HLint warnings as fatal errors. As the HLint author says "Do not blindly apply the output of HLint". String and [Char] are exactly the same, as everyone says, it's a question of which looks nicer. I would tend to use String if I'm operating on contiguous lists of characters I want to treat as a block (most of the time), and explicitly use [Char] when the characters don't make sense combined in a run (far rarer). HLint divides all hints into error (fix) and warning (think), so perhaps it might be best only to build fail on error hints.

Neil Mitchell
  • 9,090
  • 1
  • 27
  • 85