我确实有以下代码:
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")我确实有以下测试功能:
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")我现在的问题是,在输出字符串(如测试函数testMenuC中所示)时,如何去掉Char 'd‘中的引号。
发布于 2021-10-27 09:34:25
您可以将菜单的部分替换为:
menu x _ = Left ("(" ++ [x] ++ ")" ++ "is not supported, use (p)refix or (s)uffix")甚至是
menu x _ = Left . mconcat $ ["(", [x], ")", "is not supported, use (p)refix or (s)uffix"]https://stackoverflow.com/questions/69735613
复制相似问题