0

I have searched pursuit, only two of them seems matchs well :

  • charList from purescript-optlicative (module: Node.Optlicative.Internal)
  • toChars from purescript-yarn (module: Data.String.Yarn)

And both yarn and optlicative is not available with psc-package (using psc-package 0.4.0 and {"set": "psc-0.12.0", "source": "https://github.com/purescript/package-sets.git"} )

Related question: How do I convert a list of chars to a string in purescript

luochen1990
  • 3,689
  • 1
  • 22
  • 37

2 Answers2

3

I would first convert to Array Char via toCharArray, then convert to list:

import Data.List as List
import Data.String.CodeUnits as String

...

List.fromFoldable $ String.toCharArray "abcd"

NOTE: as of purescript-strings v4.0.0, toCharArray is exported from Data.String.CodeUnits, but before that it was in Data.String. Adjust according to the compiler/library version you're using.

Incidentally: are you sure you need a list and not an array? Lists are way less idiomatic in PureScript than in Haskell. Arrays are way more common.

Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
  • this piece of code not works for me, could you please add the `import` sentence also? – luochen1990 Jul 03 '18 at 16:23
  • 1
    Ok, I have added the `import` lines. However, if you require this high level of guidance, I suggest you ready some tutorials and work your way through some examples first, before writing your own code. – Fyodor Soikin Jul 05 '18 at 23:54
0

I'm going to have to write an answer instead of a comment to the previous answer because I don't yet have 50 reputation points. 50 reputation points are required before I can comment.

To convert a string to a list of characters, you can use the following code:

import Data.String.CodeUnits (toCharArray)  --from package purescript-strings@4.0.0
import Data.List (fromFoldable, List)
import Data.Function ( ($) )                --from package purescript-prelude@4.0.1

convertStringToListOfChars :: String -> List Char
convertStringToListOfChars str = fromFoldable $ toCharArray str

From the REPL, using it gives the following result:

> convertStringToListOfChars "abcde"
('a' : 'b' : 'c' : 'd' : 'e' : Nil)