递归列表压缩器在Elm中给出了奇怪的结果

时间:2016-06-29 18:59:50

标签: list elm

我有一个字典,其中包含一系列名称为“ls-1”,“ls-2”,“ls-3”等的键,还有一个键“ls-count”表示有多少键。当其中一个被删除时,说“ls-2”被删除,我想把剩余部分冒泡,以便旧的“ls-3”将变成“ls-2”,依此类推。

(是的,我知道这是存储列表的一种非常尴尬的方式,但这是因为基础数据格式。请不要XY我。)

目前用于此目的的代码是:

updateDeleteField : String -> Model -> Model
updateDeleteField key model =
    let
        keyhyphenindex =
            String.indexes "-" key

        realKeyHyphenIndex =
            case (List.head keyhyphenindex) of
                Nothing ->
                    Debug.crash "Missing hyphen in expandable form deletion process"

                Just x ->
                    x

        keyPrehyphen =
            (String.slice 0 realKeyHyphenIndex key) ++ "-"

        countName =
            Debug.log "name of the count variable:" (keyPrehyphen ++ "count")

        removeResponse =
            Debug.log "remove the deleted item:" (killResponse model key)

        reduceCount =
            Debug.log "reduce the count:" (setResponse removeResponse countName (toString ((getResponseInt removeResponse countName 0) - 1)))

        -- Integrity bubble: check if we've reached the end of the list, if so we're done
        checkInteg value m =
            if value >= ((getResponseInt m countName 0)) then
                Debug.log "Completing.." m
            else
                let
                    -- Name of the current item
                    active =
                        Debug.log "active" (keyPrehyphen ++ (toString value))

                    -- Name of the next item
                    next =
                        Debug.log "next" (keyPrehyphen ++ (toString (value + 1)))

                in
                    case (Dict.get active m.character) of
                        -- If the current item is not missing, recurse onto the rest of the numbers
                        Just x ->
                            Debug.log ("Active is ok so moving on to " ++ (toString (value + 1))) (checkInteg (value + 1) m)

                        -- If the current item is missing, look at the next one
                        Nothing ->
                            case (Dict.get next m.character) of
                                -- If the next one is present, move it down to fill the current gap, then recurse
                                Just higher ->
                                    Debug.log ("Active is missing, bubbling" ++ next ++ " to " ++ active)
                                        (checkInteg (value) (setResponse (killResponse m next) active higher))

                                -- That being missing too should be impossible since only one thing can be deleted at a time
                                Nothing ->
                                    Debug.crash ("More than one form item deleted at once? " ++ active ++ " unset, so is " ++ next ++ ", deleted key is " ++ key)
    in
        checkInteg 1 reduceCount

调用的函数和数据结构的相关部分:

type alias Model =
    { character : Dict String String ... }

getResponse : Model -> String -> Maybe String
getResponse model key =
    Dict.get key model.character

setResponse : Model -> String -> String -> Model
setResponse model key value =
    let
        char = model.character
    in
        { model | character = Dict.insert key value char }

killResponse : Model -> String -> Model
killResponse model key =
    let
        char = model.character
    in
        { model | character = Dict.remove key char }

getResponseInt : Model -> String -> Int -> Int
getResponseInt model key default =
    case (Dict.get key model.character) of
        Nothing -> default
        Just x -> case (toInt x) of
            Err _ -> default
            Ok i -> i

checkInteg的一切工作正常,但此时调试输出变得疯狂。它通常会在任何其他消息之前打印完成的消息,或者只是在无缘无故地冒泡后停止,或者说它冒泡但随后没有改变任何东西。谁能告诉我什么是错的?

0 个答案:

没有答案