Skip to content

Recursive error in foldl #1584

@dustinfarris

Description

@dustinfarris

I stumbled on this, not trying to be recursive, but just by typoing a function name.

Here's a simple example:

https://ellie-app.com/WFJM2TJRjfa1/0

main =
    let
        numbers = [1,2,3]
        sum =
            List.foldl (\num total -> num + sum) 0 numbers
    in
        text ("The sum is " ++ (toString sum))

In this simple "sum" example, we accidentally use sum in the fold function, instead of the accumulator total. But, sum happens to be the function name wrapping the entire result—therefore the compiler does not complain.

This simple example results in output: "The sum is NaN"


A slightly more complex example:

https://ellie-app.com/WFXxpjMnDka1/0

In the fold operation here

updatedUserStore =
    List.foldl 
        (\(id, name) users -> Dict.insert id name updatedUserStore) 
        userStore
        userData

on the third line:

Dict.insert id name updatedUserStore

should actually be

Dict.insert id name users

but, again, because the name is the same as the wrapping function name, Elm does not throw an error. However, this results in a runtime error (see output of Ellie)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions