求大神解答
haskell吧
全部回复
仅看楼主
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 11
[惊讶]围观
2016年04月23日 01点04分 4
level 13
忘了(´・ω・`)
2016年04月23日 10点04分 5
[怒]快想
2016年04月24日 00点04分
@魔術方块黄 ┐(´-`)┌
2016年04月24日 04点04分
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
level 10
解析字符串的工作还是交给parsec吧
2020年09月05日 15点09分 7
1