level 1
输入“A1UA2B1NB2”,输出[A1, U, A2, B1, N, B2]
应该怎样写这段程序?
2016年04月20日 00点04分
1
level 1
data Turn = A1 | A2 | B1 | B2 | U | N
getTurn :: String -> Maybe [Turn]
2016年04月20日 00点04分
3
level 1
看描述不知道楼主想干啥,下面算是个直观的解法:
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)
2016年04月25日 01点04分
6