0

The below code gives back a list of String but I want it work on multiple cases. The problem is that I can't create the same exact result with recursion. The program gives back the following result:

replaceTabs 6 ["\thello world"]

=> ["      hello world"]

Now this should work with a longer list like:

replaceTabs 6 ["asd dsa","\thello world"]

    => ["asd dsa","      hello world"]

Simple concat doesn't work, because it will give back undefined pattern.

replaceTab' :: Int -> [[Char]] -> [Char]
replaceTab' n [[x]] =
 if x == '\t' then replicate n ' '
 else [x]

replaceTabs :: Int -> [String]-> [String]
replaceTabs n [""] = [""]
replaceTabs n (x:xs) = (return . concat $ [replaceTab' n [a] | a <- (map (:[]) (x))])

1 Answer 1

3

This

replaceTab' :: Int -> [[Char]] -> [Char]

is the same as,

replaceTab' :: Int -> [String] -> String

What you should focus on is implementing a function,

replaceTab :: Int -> String -> String

which "fixes" a single String. Then replaceTabs is simply,

replaceTabs :: Int -> [String] -> [String]
replaceTabs n = map (replaceTab n) 
Sign up to request clarification or add additional context in comments.

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.