Home > OS >  How can I convert a String into a Char list?
How can I convert a String into a Char list?

Time:01-04

I am trying to write a program that removes a specific element from a string, but most of the things I use (like filter) only work for [Char]. I really just don't want to have to type "['h','e','l','l','o']" instead of "hello". I realize that technically a String is just a fancy [Char], but how would I unfancify it into a standard [Char]. Also if you have another way to write normal words instead of in an array format please tell me.

CodePudding user response:

As was already said, String is simply a synonym for [Char]

type String = [Char]

so both can be used interchangeably.

In particular, "hello" :: [Char] is exactly the same as "hello" :: String, both are just more elegant ways of writing ['h','e','l','l','o'].

That said, you'll find that not everything that would be a “String” in other languages is a String in Haskell. See, the list implementation is actually really inefficient in particular memory-wise – for an ASCII string, most languages take either 8 or 16 bit per character, but with Haskell's String type each character is a 64-bit Char plus a reference to the next character, for a total 128 bits!

That's why most modern Haskell libraries avoid String, except for short things like file names. (Incidentally,

type FilePath = String

so that is also interchangeable.)

What these libraries use for general string is typically Text, which is indeed a different type, corresponding more to other languages' implementations (it uses UTF-16 under the hood).

If you want to filter a value of that type, you can either convert it to a listy-String with unpack, or you can simply use the dedicated version of filter provided by the text library.

In standard Haskell, Text values can not be defined as string- or list literals, you'd need to explicitly wrap that like pack ['h','e','l','l','o']. However they can still be defined with a simple string literal, provided that you turn on {-# LANGUAGE OverloadedStrings #-}:

ghci> :m  Data.Text
ghci> "hello" :: Text

<interactive>:5:1: error:
    • Couldn't match expected type ‘Text’ with actual type ‘[Char]’
    • In the expression: "hello" :: Text
      In an equation for ‘it’: it = "hello" :: Text

ghci> :set -XOverloadedStrings 
ghci> "hello" :: Text
"hello"

With another extension, this also works for the list syntax:

ghci> ['h','e'] :: Text

<interactive>:9:1: error:
    • Couldn't match expected type ‘Text’ with actual type ‘[Char]’
    • In the expression: ['h', 'e'] :: Text
      In an equation for ‘it’: it = ['h', 'e'] :: Text

ghci> :set -XOverloadedLists 
ghci> ['h','e'] :: Text
"he"

CodePudding user response:

In Haskell, the square brackets mean a list, as they also do in Python. Haskell also uses white space syntax.

You can tell what type a String is in Haskell by using :t in the ghci REPL.

:t "String" -- "String" :: [Char]

So a string, in double quotes, really is a list of characters.

How about a list of strings?

:t ["airplane","boat","car"] -- ["airplane","boat","car"] :: [[Char]]

So a list of strings is a list of lists of characters.

As for filtering, if I apply a filter to a string, it behaves exactly as a filter on a list of characters:

:m  Data.Char
filter isUpper "String" -- returns "S"
  •  Tags:  
  • Related