Elm Dict中的校验值

Elm Dict中的校验值,elm,Elm,试图检查水果的价值 fruit = Dict.fromList [ ( 1, { fruitIsGood = False } ) , ( 2, { fruitIsGood = False } ) , ( 3, { fruitIsGood = True } ) ] whichFruitIsGood : Dict.Dict number { fruitIsGood : Bool } -> String whichFruitIsGood fr

试图检查水果的价值

fruit =
  Dict.fromList
      [ ( 1, { fruitIsGood = False } )
      , ( 2, { fruitIsGood = False } )
      , ( 3, { fruitIsGood = True } )
      ]

whichFruitIsGood : Dict.Dict number { fruitIsGood : Bool } -> String
whichFruitIsGood fruit =
    case get 0 fruit of
        Nothing ->
            Debug.crash "nothing found"

        Just fruit ->
            if fruit.fruitIsGood == True then
                "Apple"
            else
                "I hate Fruit"
我不知道如何获取fruitIsGood道具或您在Elm中称之为的任何东西。

首先,
Debug.crash“nothing found”
不会为您提供任何有用的功能,不如返回
nothing found
字符串

然后您只需要修复编译器指出的错误。它们主要是关于变量的,这些变量被多次定义。让我们将第一个函数重命名为
fruits

fruits =
  Dict.fromList
      [ ( 1, { fruitIsGood = False } )
      , ( 2, { fruitIsGood = False } )
      , ( 3, { fruitIsGood = True } )
      ]
以及第二个函数中的变量:

whichFruitIsGood : Dict.Dict number { fruitIsGood : Bool } -> String
whichFruitIsGood fruit =
    case get 3 fruit of
        Nothing ->
            "nothing found"

        Just foundFruit ->
            if foundFruit.fruitIsGood == True then
                "Apple"
            else
                "I hate Fruit"
然后,您的代码将编译并返回
未找到任何内容

这里有一个稍加修改的示例,它显示了实际运行的代码