首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >怎么才能把这个字符串列表转换成列表呢?

怎么才能把这个字符串列表转换成列表呢?
EN

Stack Overflow用户
提问于 2022-12-04 13:57:28
回答 1查看 35关注 0票数 1

该字符串如下所示:

代码语言:javascript
运行
复制
",w84,w41,w56,w170,w56,w41,w84,/,,w24,w40,w17,w40,w48,,/ ,,,w16,w16,w16,,,/,,,,,,,,/,,,,,,,,/,,,,,,,,/,,,b1,b1,b1,,,/ ,,b3,b130,b17,b130,b129,,/,b69,b146,b131,b170,b131,b146,b69,"

但应该是这样的

代码语言:javascript
运行
复制
[[Empty,Piece White 84,Piece White 41,Piece White 56,Piece White 170,Piece White 56,Piece White 41,Piece White 84,Empty],[Empty,Empty,Piece White 24,Piece White 40,Piece White 17,Piece White 40,Piece White 48,Empty,Empty],[Empty,Empty,Empty,Piece White 16,Piece White 16,Piece White 16,Empty,Empty,Empty],[Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty],[Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty],[Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty],[Empty,Empty,Empty,Piece Black 1,Piece Black 1,Piece Black 1,Empty,Empty,Empty],[Empty,Empty,Piece Black 3,Piece Black 130,Piece Black 17,Piece Black 130,Piece Black 129,Empty,Empty],[Empty,Piece Black 69,Piece Black 146,Piece Black 131,Piece Black 170,Piece Black 131,Piece Black 146,Piece Black 69,Empty]]

我的代码创建的列表如下所示:

[“空”、“白”、“空、空、白24、白40、白17、白40、白48、空、空”、“空、空”、“空”

代码语言:javascript
运行
复制
data Player = Black | White deriving Show
data Cell = Piece Player Int | Empty deriving Show
data Pos = Pos { col :: Char, row :: Int } deriving Show
type Board = [[Cell]]

我有这些数据类型。

我几乎完成了这个任务,我所需要的就是去掉引号。

到目前为止,这是我的代码:

代码语言:javascript
运行
复制
buildBoard x =  rec(help3(wop(helper (replaceO x))))


wop (x:xs) = splitOn "/" (x:xs)

help3 (x:xs) = map (\x -> [x])(x:xs)

rec (x:xs) = map(\x -> [recH(x)])(x:xs)

recH (x:xs) = checkComma(x)

helper (x:y:xs) 
            |x == ',' && y == ',' = x:'E':'m':'p':'t':'y':helper(y:xs)
            |otherwise = x:helper (y:xs)
helper [] = []
helper [x] = [x]

checkComma (x:xs) = if head (x:xs) == ',' then checkComma('E':'m':'p':'t':'y':',':xs) else if last (x:xs) == ',' then reverse(turnAr(reverse(x:xs))) else (x:xs)

turnAr (x:xs) = 'y':'t':'p':'m':'E':',':xs

replaceO [] = []
replaceO (x:xs) = 
     if x == 'w' 
      then 'P':'i':'e':'c':'e':' ':'W':'h':'i':'t':'e':' ': replaceO xs 
     else if x == 'b'
      then 'P':'i':'e':'c':'e':' ':'B':'l':'a':'c':'k':' ': replaceO xs
     else if x == 'E'
      then 'E':'m':'p':'t':'y':' ': replaceO xs
     else x : replaceO xs
EN

回答 1

Stack Overflow用户

发布于 2022-12-04 16:10:15

仅仅去掉引号不是一件容易的事。

我认为您必须对代码进行一些重大更改。我建议先执行splitOn "/",然后再执行splitOn ",",它生成一个字符串列表,每个字符串代表一个单元格。

然后,您可以很容易地编写一个函数parseCell :: String -> Cell来解析这些内部单元格。这个函数将有点像您的replaceO函数,但它也应该处理所有空单元格并实际解析整数(您可以使用read函数)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74677284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档