简体   繁体   中英

Haskell string/output manipulation

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).

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"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM