成功编译的 Elm 代码显示 "too much recursion" 警告

Successfully compiled Elm code shows "too much recursion" warning

我使用的是我自己编写的 Toolbox 模块,它编译时没有任何错误或警告。但是,当调用 menubar 函数时,控制台会显示 "too much recursion" 警告。

Toolbox.elm

module Toolbox exposing (..)

import List exposing (head, tail)
import Maybe exposing (withDefault)
import Html exposing (..)
import Html.Attributes exposing (..)


menubar : Html Never
menubar =
    div
        [ class "relative bg-dark-blue firasans white ttu pa2" ]
        [ img
            [ src "i/balloon.png"
            , width 45
            , height 73
            ]
            []
        , ul
            [ class "dt absolute bl-l b--white list pa1 ml3 mt2"
            , style
                [ ( "left", "45px" )
                , ( "top", "0" )
                , ( "height", "73px" )
                ]
            ]
            (imgList
                [ class "dtc v-mid" ]
                []
                [ "i/home.png"
                , "i/tutorials.png"
                ]
            )
        ]


imgList : List (Attribute msg) -> List (Maybe (Attribute msg)) -> List String -> List (Html msg)
imgList attrs events srcs =
    case (head events) of
        Nothing ->
            (li
                attrs
                [ img [ src (wdEmptyStr (head srcs)) ] [] ]
            )
                :: (imgList attrs [] (wdEmpty (tail srcs)))

        Just maybe ->
            case maybe of
                Nothing ->
                    (li
                        attrs
                        [ img [ src (wdEmptyStr (head srcs)) ] [] ]
                    )
                        :: (imgList attrs (wdEmpty (tail events)) (wdEmpty (tail srcs)))

                Just event ->
                    (li
                        (event :: attrs)
                        [ img [ src (wdEmptyStr (head srcs)) ] [] ]
                    )
                        :: (imgList attrs (wdEmpty (tail events)) (wdEmpty (tail srcs)))


wdEmpty =
    withDefault []


wdEmptyStr =
    withDefault ""

刚去查看 Javascript 控制台,看到一个 "too much recursion" 警告。

通过重写 Toolbox.imgList 修复:

imgList : List (Attribute msg) -> List (Maybe (Attribute msg)) -> List String -> List (Html msg)
imgList attrs events srcs =
    case srcs of
        [] ->
            []

        _ ->
            case (head events) of
                Nothing ->
                    (li
                        attrs
                        [ img [ src (wdEmptyStr (head srcs)) ] [] ]
                    )
                        :: (imgList attrs [] (wdEmpty (tail srcs)))

                Just maybe ->
                    case maybe of
                        Nothing ->
                            (li
                                attrs
                                [ img [ src (wdEmptyStr (head srcs)) ] [] ]
                            )
                                :: (imgList attrs (wdEmpty (tail events)) (wdEmpty (tail srcs)))

                        Just event ->
                            (li
                                (event :: attrs)
                                [ img [ src (wdEmptyStr (head srcs)) ] [] ]
                            )
                                :: (imgList attrs (wdEmpty (tail events)) (wdEmpty (tail srcs)))