首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Array.set期望得到Array.Array Char,但可能得到(Array.Array Char)

Array.set期望得到Array.Array Char,但可能得到(Array.Array Char)
EN

Stack Overflow用户
提问于 2018-09-11 17:52:02
回答 1查看 50关注 0票数 0

不知道这到底是什么问题。我是榆树的新手,所以别紧张。

代码语言:javascript
运行
复制
board = [ [ 'P', 'P', ' ' ], [ ' ', ' ', ' ' ], [ ' ', ' ', ' ' ] ]

move = nextBestMove board


nextBestMove : List (List Char) -> Int
nextBestMove gameNode =
    let
        node =
            map fromList gameNode

        -- node is now List (Array.Array Char)

        currentBoard =
            fromList node

        -- currentBoard is now Array.Array (Array.Array Char)

        row1 =
            get 0 currentBoard

        -- row1 is now Maybe.Maybe (Array.Array Char)

        -- now I want to place an X for the empty value in [ 'P', 'P', ' ' ]
        row1NextState =
            set 2 'X' row1

... rest of code

我得到的类型不匹配错误是:

代码语言:javascript
运行
复制
The 3rd argument to function `set` is causing a mismatch.

22|             set 2 'X' row1
                          ^^^^
Function `set` is expecting the 3rd argument to be:

    Array.Array Char

But it is:

    Maybe (Array.Array Char)

我不明白为什么我现在认为我有一个二维数组,它应该是好的。我想要做的是得到一个指向我的棋盘第一行的指针。所以我想从本质上说,这样我就可以更新行[ 'P', 'P', ' ' ]中的空位置了

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-11 18:03:38

正如您已经注意到的,到达函数将产生一个Maybe类型。这意味着我们需要将Maybe类型转换为Array类型,以便在set中使用它。我们可以在这里使用像withDefault这样的函数。

代码语言:javascript
运行
复制
    row1NextState =
        set 2 'X' (withDefault (Array.initialize 3 (always ‘X’)) row1)

这使得榆树在row1Nothing的情况下使用Nothing

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

https://stackoverflow.com/questions/52281595

复制
相关文章

相似问题

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