Discussion:
[elm-discuss] why does this code compile?
Maris Orbidans
2017-11-26 20:54:21 UTC
Permalink
https://gist.github.com/maruks/18fb2a23e1a2d019dc37563aa08ee46f



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
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Aaron VonderHaar
2017-11-26 23:21:57 UTC
Permalink
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.)
Post by Maris Orbidans
https://gist.github.com/maruks/18fb2a23e1a2d019dc37563aa08ee46f
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
--
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
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Gabriel Sartori
2017-11-26 23:46:35 UTC
Permalink
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?
https://ellie-app.com/kR68N5KWGa1/0
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Maris Orbidans
2017-11-28 11:34:48 UTC
Permalink
It makes sense if something is lazy evaluated.
Post by Gabriel Sartori
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?
https://ellie-app.com/kR68N5KWGa1/0
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...