For example:
wordsToString ["all","for","one","and","one","for","all"]
"all for one and one for all"
My code works without a type declaration:
wordsToString [] = ""
wordsToString [word] = word
wordsToString (word:words) = word ++ ' ':(wordsToString words)
But when I do the type check,it shows that it is a list of Chars which seems wrong to me as I'm supposed to declare the input as a list of strings and get a string as the output:
*Main> :type wordsToString
wordsToString :: [[Char]] -> [Char]
I want to change the declaration to wordsToString::[(String)]->[String]
but it won't work
I want to change the declaration to
wordsToString::[(String)]->[String]
but it won't work
No, you want to change the declaration to wordsToString :: [String] -> String
. You aren't getting a list of strings out, just a single one.
The function is called concat
:
concat :: Foldable t => t [a] -> [a]
concat xs = foldr (++) [] xs
In your case, you want to insert a whitespace between the characters. This function is called intercalate
:
intercalate :: [a] -> [[a]] -> [a]
It's defined in terms of intersperse
.
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.