Home > Software design >  Haskell string/output manipulation
Haskell string/output manipulation

Time:10-28

I do have the following code:

suffixes :: [a] -> [[a]]
suffixes [] = [[]]
suffixes l@(_:t) = l : suffixes t

prefixes :: [a] -> [[a]]
prefixes [] = [[]]
prefixes l@x = l : prefixes (init x)

menu :: Char -> [a] -> Either String [[a]]
menu 'p' l = Right (prefixes l)
menu 's' l = Right (suffixes l)
menu x _ = Left ("("    show x    ")"    "is not supported, use (p)refix or (s)uffix")

I do have the following test function:

testMenuP = "Expected Right [[1,2],[1],[]]; menu 'p' [1,2] returned "    show (menu 'p' [1,2] :: Either String [[Int]])

testMenuS = "Expected Right [[1,2],[2],[]]; menu 's' [1,2] returned "    show (menu 's' [1,2] :: Either String [[Int]])

testMenuC = "Expected Left \"(d) is not supported, use (p)refix or (s)uffix\"; menu 'd' [1,2] returned "    show (menu 'd' [1,2] :: Either String [[Int]])

testMenu = putStr (testMenuP    "\n"    testMenuS    "\n"    testMenuC    "\n")

My question is now, how do I get rid of the quotes '' in the Char 'd' when I output the string (as shown in the test function testMenuC).

CodePudding user response:

You can replace the part of menu with:

menu x _ = Left ("("    [x]    ")"    "is not supported, use (p)refix or (s)uffix")

or even

menu x _ = Left . mconcat $ ["(", [x], ")", "is not supported, use (p)refix or (s)uffix"]
  • Related