如何更改Haskell中[String]中的元素? [英] How to change an element in [String] in Haskell?

查看:134
本文介绍了如何更改Haskell中[String]中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ##### 
#_ ##
###
##
#。#
#####
1 4(玩家初始位置,用' _')

接收到输入后,程序将其转换为[String]。



这种情况下,会是:
[#####,#_ ##,###,##, #。#,#####,1 4]



如何访问position [1,4]并将'_'转换为'O'?
函数必须返回具有该变换的初始列表。



非常重要的注意事项:'_'从不显示在输入上,我只用它来明确位置[1,4]的位置(因此,输入时我们只能看到一个空格,'')

看起来你可能需要为在线编码游戏解决这些任务之一。正如其他人指出的,列表并不适合处理这样的坐标。但是,如果你不能使用更好的库(比如在编码游戏中),你将不得不做更多的工作。



这里是我的 ghci session(转换为正确的程序留给读者练习......):

  let input = [#####,#_ ##,###,##,#。#,#####, 1 4] 
let reverseInput =反向输入
let position = head reverseInput
let board = tail reverseInput
let posX = read $ takeWhile(/ ='')position :: Int
let posY = read $ takeWhile(/ ='')$反向位置:: Int
let(unchangedBoard,changedBoard)= splitAt posY board
let(unchangedRow,changedRow)= splitAt posX $ head changedBoard
let newRow = unchangedRow ++o++ tail changedRow
let newBoard = unchangedBoard ++ [newRow] ++ tail changedBoard
let finalOutput = reverse newBoard
mapM_ putStrLn finalOutput

还要注意这段代码非常brittl因为它在整个地方使用了部分函数( tail head read )。您可以尝试使用模式匹配,而不是使代码更健壮。


I'm working on a program that receives as input a board game as follows:

#####
#_ ##
#  ##
#   #
#  .#
#####
1 4 (player initial position, marked with '_')

After receiving the input, the program transforms it to a [String].

This case, it would be: ["#####", "#_ ##", "# ##", "# #", "# .#", "#####", "1 4"]

How can I access position [1,4] and transform '_' to 'o'? Function must return initial list with that transformation.

Very important note: '_' is never displayed on input, I only used it to make clear where position [1,4] is (therefore, on input we only see a blank space, ' ')

解决方案

Seems like one of those tasks you might have to solve for online coding games. As others pointed out, lists are not really suited for dealing with coordinates like this. However, if you are not able to use better libraries (like in coding games) you will have to do some more work.

Here is the code from my ghci session (transforming to a proper program is left as an exercise for the reader...):

let input =  ["#####", "#_ ##", "#  ##", "#   #", "#  .#", "#####", "1 4"]
let reverseInput = reverse input
let position = head reverseInput
let board = tail reverseInput
let posX = read $ takeWhile (/=' ') position :: Int
let posY = read $ takeWhile (/=' ') $ reverse position :: Int
let (unchangedBoard, changedBoard) = splitAt posY board
let (unchangedRow, changedRow) = splitAt posX $ head changedBoard
let newRow = unchangedRow ++ "o" ++ tail changedRow
let newBoard = unchangedBoard ++ [newRow] ++ tail changedBoard
let finalOutput = reverse newBoard
mapM_ putStrLn finalOutput

Also note this code is very brittle as it uses partial functions all over the place (tail, head, read). You could try to use pattern matching instead to make the code more robust.

这篇关于如何更改Haskell中[String]中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆