Monday 6 June 2016

Haskell: Convert list of words to a sentence.

Prelude> let wordsToSentence list = [c | word <- list, c <- word]
Prelude> 
Prelude> wordsToSentence ["Hello", "PTR", "How", "are", "you"]
"HelloPTRHowareyou"


Observe above output, it is not looks like a sentence, let me add some space between words.
Prelude> let wordsToSentence list = [c | word <- list, c <- ' ' : word]
Prelude> 
Prelude> wordsToSentence ["Hello", "PTR", "How", "are", "you"]
" Hello PTR How are you"


Previous                                                 Next                                                 Home

No comments:

Post a Comment