Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a String into a Char list?

Tags:

haskell

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.

like image 552
Amber Avatar asked Jan 03 '22 05:01

Amber


People also ask

Can you turn a string into a char?

We can convert String to char in java using charAt() method of String class. The charAt() method returns a single character only. To get all characters, you can use loop.

Can you convert a string to a list?

Strings can be converted to lists using list() .

How do you convert a string into a list of characters in Python?

To convert a string to list of characters in Python, use the list() method to typecast the string into a list. The list() constructor builds a list directly from an iterable, and since the string is iterable, you can construct a list from it.

How to convert a string to a list of characters?

So, let us explore the 7 different ways to achieve this conversion. The split method by default takes whitespace as delimiter and separates the words of the string from by the whitespace and converts them into a list. To convert a string into list of characters, we can simply use type conversion using the inbuilt list () method.

How to initialize a list<char> with a string in C++?

Alternatively, you can use the List constructor to initialize a new instance of the List<char> class with characters of a string. You can simply pass a string to the list constructor, as the following example illustrates: If you want a list of strings, then you would use a List<string> rather than List<char>.

How do I convert a string to a list in Python?

Python | Program to convert String to a List. In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the users choice, are encountered. To do this we use the split() method. Syntax: Examples: The split method is used to split the strings and store them in the list.

How to get a list<char> in Java?

To get a List<char>, do like: If you need a List<string> instead to store the characters, convert each character to a string first: 2. Using List Constructor Alternatively, you can use the List constructor to initialize a new instance of the List<char> class with characters of a string.


Video Answer


2 Answers

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"
like image 63
leftaroundabout Avatar answered Oct 28 '22 14:10

leftaroundabout


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"
like image 40
Francis King Avatar answered Oct 28 '22 15:10

Francis King