1

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

2
  • I think your question could be replaced with: show 'c' returns "'c'", how do I get "c" instaed? Or How do I convert a Char to a String. And the answer would be: use ['c'] instead of show 'c'. Commented Oct 27, 2021 at 9:07
  • 1
    Please remove the code that is not absolutely important. This makes it easier to spot your question. Commented Oct 27, 2021 at 9:08

1 Answer 1

2

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