成功编译的Elm代码显示“过多的递归”警告

问题描述 投票:0回答:1

我正在使用我自己编写的Toolbox模块,它编译时没有任何错误或警告。但是,当调用menubar函数时,控制台会显示“过多的递归”警告。

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 ""
elm
1个回答
1
投票

刚刚去了Javascript控制台,看到了“太多的递归”警告。

通过这种方式重写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)))
© www.soinside.com 2019 - 2024. All rights reserved.