得到一个Maybe而不是Array的值

Pos*_*Guy 2 elm

榆树新来到这里,起初它让我绝对疯狂,不知道这种挑剔语言的来龙去脉(即使在阅读了关于它的sh**负载因为它只是如此不同和挑剔......我猜这是本质的一个功能性的lang)所以当你尝试做一件简单的事情时,就像起初拉头发一样.

我收到以下错误:

The right side of (==) is causing a type mismatch.

29|             get 0 arrayOfValues == 'X'
                              ^^^
(==) is expecting the right side to be a:

    Maybe Char

But the right side is:

    Char

Hint: With operators like (==) I always check the left side first. If it seems
fine, I assume it is correct and check the right side. So the problem may be in
how the left and right arguments interact.
Run Code Online (Sandbox Code Playgroud)

测试:

it "blah blah blah" <|
    let
        someArray =
            [ 'P', ' ' ]
    in
expect (MyModule.doSomething someArray 'P') to equal 1
Run Code Online (Sandbox Code Playgroud)

MyModule的

doSomething : List Char -> Char -> Int
doSomething arrayOfValues symbol =
    let
        grid =
            fromList arrayOfValues

        found =
            get 0 arrayOfValues == symbol
    in
    if found then
        1
    else
        0
Run Code Online (Sandbox Code Playgroud)

现在我假设但不确定,当我试图从我的阵列中拉出第一个值但是不确定时,它正在变为Nothing或者什么东西.也许Char我假设没有回来?唐诺,也许还有其他问题.

我想让上面的代码工作,然后重构......我确定可能有一种更优雅的方式来编码我上面编码的内容,但首先是第一件事,修复此错误并使用现有代码更好地理解它.对于如何以及如何处理,错误消息虽然好看并不明显.我有假设,但不完全确定如何处理导致问题的任何问题.

Dog*_*ert 6

Array.get返回包含在a中的值,Maybe因为数组中指定的索引可能没有值.如果要检查索引0处的值是否存在等于'X',则可以比较Just 'X':

get 0 arrayOfValues == Just 'X'
Run Code Online (Sandbox Code Playgroud)

就像错误消息所说,编译器发现左侧==是a Maybe Char,右侧是Char.您需要将一个转换为另一个使用==.在这种情况下,您可能想要改变右侧,如上所述.