why does this code compile?

160 views
Skip to first unread message

Maris Orbidans

unread,
Nov 26, 2017, 5:22:50 PM11/26/17
to Elm Discuss





blur : Dict(Int,Int) Int -> ( Int, Int ) -> Int
blur cells ( x, y ) =
    let
        xs =
            range (x - 1) (x + 1)

        ys =
            range (y - 1) (y + 1)

        points =
            concatMap (\x -> map (\y -> ( x, y )) ys) xs

        ( sum, num ) =
            foldr
                (\p ( s, n ) ->
                    case Dict.get p cells of
                        Just h ->
                            ( sum + h, num + 1 ) --  ( s + h, n + 1 )

                        Nothing ->
                            ( s, n )
                )
                ( 0, 0 )
                points
    in
    sum // num

Aaron VonderHaar

unread,
Nov 26, 2017, 6:22:22 PM11/26/17
to elm-d...@googlegroups.com
I'm assuming you're expecting an error because `sum` and `num` are values that are referred to within their own definition?

The rule the compiler currently follows is that referring to yourself is allowed if the reference is inside a lambda.  (In this example, the compiler does know whether or not `foldr` is going to immediately call the lambda or leave it unevaluated.)

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gabriel Sartori

unread,
Nov 26, 2017, 6:56:25 PM11/26/17
to Elm Discuss
Look at this simpler example, like Aaron said, the compiler does not complain if the value is referred inside a lambda. But I think there is no reason to allow this, or I am missing something?
 

Maris Orbidans

unread,
Nov 28, 2017, 7:57:55 AM11/28/17
to Elm Discuss

It makes sense if something is lazy evaluated.   
Reply all
Reply to author
Forward
0 new messages