0

I want a fuction where you put in a String like "Hello I am 25 years old!" and get a list out of it like ["hello","i","am","years","old"]. So it should put all uppercase letters in lowercase and delete everything that isnt a letter. It should just use Data.List and Data.Char. I know i should use words on the String and then filter it but i just cant figure it out (yes im new to Haskell).

toString :: String -> [String]
toString str = ...
1
  • Something like filter (not . null) . map (filter Data.Char.isLower . map Data.Char.toLower) . words. Think of it as three stages written right-to-left: break on spaces, within each word, convert to lower and prune out extras, then finally eliminate the empty words. Commented Nov 11, 2014 at 19:34

2 Answers 2

2

At the risk of answering a homework question:

import Data.Char

toString :: String -> [String]
toString str = filter (not . null) . map (map toLower . filter isAlpha) . words $ str

Prelude Data.Char> toString "Hello I am 25 years old!"
["hello", "i", "am", "years", "old"]
Sign up to request clarification or add additional context in comments.

2 Comments

Had this before but I got this error so i opened this question Couldn't match expected type [String]' with actual type String -> [[Char]]' In the expression: filter (not . null) . map (map toLower . filter isAlpha) . words In an equation for `getWords': getWords str = filter (not . null) . map (map toLower . filter isAlpha) . words
If you define the str argument in your function definition, then you also need to pass it to the function implementation. It should be either getWords str = ... str or getWords = ...
0

This would be my workflow: Use hoogle and work with the type signatures you need.

  1. On how to get a list of words: You need a function foo that does "one two three" -> ["one", "two", "three"] so it has the type signature: String -> [String]. Search for exactly this type signature via hoogle and you will find the function words second in the result list.

  2. a method which does uppercase would mostly like have the type signature Char -> Char. Type this into hoogle and you will find toUpper in third in the list.

  3. is a letter: Char -> Bool

  4. next open ghci and try out the functions. IE:

    ghci> :t toUpper -- will print the type of isUpper

    <interactive>:1:1: Not in scope: ‘isUpper’ -- you need to import Data.Char

    ghci> import Data.Char -- so let´s import Data.Char

    ghci> toUpper "abc"

    ghci> words "a quick brown fox"

    ghci> :t map

    ghci> map toUpper ["a", "quick"]

... and so on

You`ll still need to figure out how to put these parts together with map and filter but again I´d advice you to have a close look at the types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.