How can I convert a String into a Char list?

Amber

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.

leftaroundabout

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"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I convert a string to char[] without copying the object?

How can I convert a String to a char array?

How can I convert a string containing a character escape sequence into a char?

How can I convert comma separated string into a List<int>

How do I convert a char to a String?

How can I convert string to dict or list?

How can I convert a one element string into a char?

How can I convert CharArray / Array<Char> to a String?

How can I convert a string list to a list of ints?

How can I convert the list string to a float

How can I convert json map to List String?

How can I convert a list to a string in Terraform?

How can I convert this random.choices python list to a string?

How can I convert a string into a list and then back to a string?

How can I convert an int to a string (char*) in C

How can I convert a List<ExpandoObject> to an XML string

Can i convert char symbol to string

How can I convert a json string to a bean list?

How can I convert a comma delimited string to a list of integers?

How can I convert a char/string in JS to uint8?

How can I select a char from a string inside a list in python and compare it with a single char?

How can I convert a list to string

How can I use recursion to convert a string to list of characters in Python?

How can I use a string instead of a char in list comprehension

How can I convert string to the list as letter and letter in python?

How can I convert a string to a list in a list? (Python)

How can I convert list of string to pandas DataFrame in Python

How can I convert a List<String> to String []?

how can i convert a string to a list to index using a for loop in lua?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive