簡體   English   中英

如何將字符的嵌套列表轉換為字符串Haskell

[英]How to convert a nested list of Chars to a String Haskell

我有一個簡單的問題,盡管Char的List似乎等效於String ,但它們在功能上並不相同。 如果我有一個嵌套列表Char S型, [[Char]]我想轉換為[String] ,我怎么會去這樣做?

當我嘗試在[[Char]]上執行函數並將其視為字符串時,我得到:

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

當我嘗試聲明type String = [[Char]]我得到:

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')

這兩個類型是完全相同的,因為String[Char]的類型同義詞。 String的定義是

type String = [Char]

type關鍵字表示它是類型的同義詞。 類型同義詞始終可以與定義為的類型互換。

您可以在GHCi中看到以下內容(請注意,Haskell不允許強制轉換):

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

當我嘗試聲明type String = [[Char]]我得到:

 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') 

之所以會出現此錯誤,是因為您對String定義與base已經定義並由Prelude導出的String定義沖突。 刪除您的定義,並使用已經存在的定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM