String manipulation haskell

Asked by At

In the following exercise i want to manipulate a random string input by using functions.

Step 1: I want to remove all characters which are not digits, letters or spaces

Step 2: I want to replace all spaces with '_'

Step 3: I want to convert all numbers to spaces

Step 4: I want to replace all 'a' with 'z' and all 'A' with 'Z'

For lists i already used the filter function and i am wondering if this function can also be used for string inputs. I am not quite sure how to approach this exercise.

Update: I found an approach to solve step 1 and step 3 but i am not quite sure how to put the different functions together in a function which includes every step. Is it possible to call the different functions one after another in the right order in some kind of main function?

    import Data.Char 

    toUpperStr xs = map toUpper xs     -- function to convert lower to upper

    dropInvalids xs = (filter (\x -> isUpper x || isSpace x || isDigit x)) $ 
    toUpperStr xs           

    replaceBlank [] = []               -- function to replace " " with "_"
    replaceBlank (x:xs) =  
             if x == ' ' 
             then '_' : replaceBlank xs 
             else x : replaceBlank xs   

1 Answers

Mark Neu On Best Solutions

Yes, absolutely! That's one of the beautiful things about Haskell.
You can treat Strings as [Char]. In fact, that's what they are!
In GHCi, type :i String - you get type String = [Char].
You can easily compose functions. There's an operator for that, (.). So (f . g) x is f (g x).

I would improve the code in a few key ways.
Firstly, make the replaceBlank function more general, so it takes a condition and a replacement function.
Secondly, compose all the functions in a "main" function, as you call it.
But do not name the main function main! That name is reserved for the IO action of a program.

It's also important not to think of the final function as "calling" the other functions.
That is imperative terminology, here, we are applying the function(s).

Also, why does your dropInvalids contain a toUpperStr? You never specified the string to be all uppercase in the end.

Also also, be sure to declare the type of your functions.

In this case, the following would be the correct code:

import Data.Char

dropInvalids :: [Char] -> [Char]
dropInvalids = filter (\x -> isLetter x || isSpace x || isDigit x)
    -- isLetter exists

replace' :: (a -> Bool) -> (a -> a) -> [a] -> [a]
replace' _ _ [] = []
replace' f g (x:xs) =
            if f x
            then g x : replace' f g xs
            else x : replace' f g xs
    -- To replace one value with another, use replace (== a) (const b).

replaceWith :: (a -> Bool) -> a -> [a] -> [a]
replaceWith f b = replace' f (const b)

replace :: Eq a => a -> a -> [a] -> [a]
replace a b = replace' (== a) (const b)
  -- The Eq makes sure you can check for equality.

manipulateString :: [Char] -> [Char]
manipulateString = replace 'A' 'Z' . replace 'a' 'z' . replaceWith isDigit ' ' . replace ' ' '_' . dropInvalids