如何删除字符串中的引号

时间:2019-04-15 11:19:39

标签: haskell

我真的是Haskell的新人。对于主编码器来说,这也许是一个简单的问题。我想删除字符串外的引号。例如“ A1”到A1。

我尽力解决了这个问题。但这不起作用。我已经使用过read :: Read a => String-> a,id函数和正则表达式。

initialGuess :: ([Pitch],GameState)
initialGuess = (startGuess, GameState (chords))
      where startGuess = ["A1", "B1", "C1"] 
      standard = [[n, o] | n <- ['A'..'G'], o <- ['1'..'3']]  <br/>
      chords = [[a,b,c] | a <- standard, b <- standard, c <-  
               standard, a /= b, b /= c, a /= c]  

initialGuess的目的是不接受任何输入参数,并返回一对初始猜测值和一个游戏状态。运行此代码,我可以得到

  

[“ A1”,“ B1”,“ C1”],GameState [[“ A1”,“ A2”,“ A3”],[“ A1”,“ A2”,“ B1”],[“ A1 “,” A2“,” B2“],[” A1“,” A2“,” B3“],[” A1“,” A2“,” C1“] ............ .... [“ A1”,“ A2”,“ C2”],[“ A1”,“ A2”,“ C3”],[“ A1”,“ A2”,“ D1”],[“ A1” ,“ A2”,“ D2”],[“ A1”,“ A2”,“ D3”]]

但是,我想删除这些引号,例如

  

[A1,B1,C1],GameState [[A1,A2,A3],[A1,A2,B1],[A1,A2,B2],[A1,A2,B3],[A1,A2, C1] ............. [A1,A2,C2],[A1,A2,C3],[A1,A2,D1],[A1,A2,D2], [A1,A2,D3]]

1 个答案:

答案 0 :(得分:1)

如果您只是想让状态更漂亮,则可以提供自己的显示功能:

-- |Display a list of strings without quotation marks
showLStr :: [String] -> String
showLStr p  = "[" ++ intercalate "," p ++ "]"

intercalate函数将","放在字符串列表的每个元素之间。

showLStr ["A1", "A2", "A3"]

应显示为

[A1,A2,A3]

编辑:现在您可以使用showLStr来显示游戏状态:

showGameState :: GameState -> String
showGameState (GameState chords) =
  "GameState [" ++ (intercalate "," $ map showLStr chords) ++ "]"

-- |Make GameState "Show"-able
instance Show GameState where
  show = showGameState

showGuess :: ([Pitch],GameState) -> String
showGuess (pitch, gameState) = showLStr pitch ++ ", " ++ show gameState
相关问题