简体   繁体   中英

How to convert a nested list of Chars to a String Haskell

I have a simple question, although Lists of Char s seem equivalent to String s, they are not functionally working the same. if I have a nested List of Char s, of type [[Char]] that I would like to convert to a [String] , how would I go about doing this?

When I try to perform a function on the [[Char]] and treat it as though it is a string I get:

Couldn't match expected type `([String] -> (Int, Int, Board)) -> t'
                with actual type `[[Char]]'

When I try to declare that type String = [[Char]] I get:

Ambiguous occurrence `String'
It could refer to either `Main.String', defined at Testing.hs:19:1
                      or `Prelude.String',
                         imported from `Prelude' at Testing.hs:16:1-14
                         (and originally defined in `GHC.Base')

Those two types are completely identical, because String is a type synonym for [Char] . The definition of String is

type String = [Char]

The type keyword means it's a type synonym. A type synonym is always interchangeable with the type it is defined to be.

You can see this in GHCi like this (note that Haskell does not allow casting):

ghci> let test :: [Char];   test = "abc"
ghci> test :: String
"abc"
ghci> :t (test :: [Char]) :: String
(test :: [Char]) :: String :: String

When I try to declare that type String = [[Char]] I get:

 Ambiguous occurrence `String' It could refer to either `Main.String', defined at Testing.hs:19:1 or `Prelude.String', imported from `Prelude' at Testing.hs:16:1-14 (and originally defined in `GHC.Base') 

You are getting this error because your definition of String conflicts with the one already defined in base and exported by the Prelude . Remove your definition and use the one that already exists instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM