Reputation: 129
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
Upvotes: 0
Views: 888
Reputation: 5658
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
.
Upvotes: 1
Reputation: 153212
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.
Upvotes: 1