Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to write an reversed words function in haskell

Write a program that reads a text in an endless loop, every single word
flipped over and then put back together as text. If the input is empty, the program should
abort, stop.
To do this, write a reverseWords::String -> String function that does the reversing
is working.

for example:

reverseWords "Hello World, 123" -> "olleH ,dlroW 321"

my code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

reverseWords :: String -> String 
reverseWords = unwords . reverse . words

doesnt work

>Solution :

The function reverse reverses the entire list, so you are getting the words in the reverse order. What you want instead, is to reverse each element separately, which can be fixed with map:

reverseWords = unwords . map reverse . words
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading