[英]a Haskell function that converts a list of words to a string
例如:
wordsToString ["all","for","one","and","one","for","all"]
"all for one and one for all"
我的代碼無需類型聲明即可工作:
wordsToString [] = ""
wordsToString [word] = word
wordsToString (word:words) = word ++ ' ':(wordsToString words)
但是當我進行類型檢查時,它表明這是一個Chars列表,這對我來說似乎是錯誤的,因為我應該將輸入聲明為字符串列表並獲取字符串作為輸出:
*Main> :type wordsToString
wordsToString :: [[Char]] -> [Char]
我想將聲明更改為wordsToString::[(String)]->[String]
但它不起作用
我想將聲明更改為
wordsToString::[(String)]->[String]
但它不起作用
不,您想將聲明更改為wordsToString :: [String] -> String
。 您不會得到一個字符串列表,只有一個。
該函數稱為concat
:
concat :: Foldable t => t [a] -> [a]
concat xs = foldr (++) [] xs
對於您的情況,您想在字符之間插入一個空格。 此函數稱為intercalate
:
intercalate :: [a] -> [[a]] -> [a]
它是根據intersperse
定義的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.