看描述不知道楼主想干啥,下面算是个直观的解法:
data Turn = A1 | A2 | B1 | B2 | U | N deriving (Show)
getTurn :: String -> Maybe [Turn]
getTurn x =
let turns = getTurns x in
if length turns == 0 then
Nothing
else
Just turns
getTurns :: String -> [Turn]
getTurns [] = []
getTurns (x:[])
| (x:[]) == show U = [U]
| (x:[]) == show N = [N]
| otherwise = []
getTurns (x:xs:xxs)
| (x:[]) == show U = U : getTurns (xs:xxs)
| (x:[]) == show N = N : getTurns (xs:xxs)
| (x:xs:[]) == show A1 = A1 : getTurns xxs
| (x:xs:[]) == show A2 = A2 : getTurns xxs
| (x:xs:[]) == show B1 = B1 : getTurns xxs
| (x:xs:[]) == show B2 = B2 : getTurns xxs
| otherwise = getTurns (xs:xxs)