Environments as Scala Functions: Problem with recBind

Jannis
Mausschubser
Mausschubser
Beiträge: 63
Registriert: 15. Apr 2015 17:10

Environments as Scala Functions: Problem with recBind

Beitrag von Jannis » 2. Mär 2018 11:35

Hi!

On slide 81 of V05-FirstClassFunctions, it is asked whether there are any problems with the following definition of recBind:

Code: Alles auswählen

def recBind(boundId: Symbol, namedExpr: Expr, env: Env): Env = {
    def recEnv: Env = { id =>
        if (id == boundId) interp(namedExpr, recEnv)
        else env(id)
    }
    recEnv
}
If I remember correctly, we said that this definition does not work because Scala does eager evaluation. Therefore we would have to add the lazy keyword as seen in RCFLAEFunEnvsInterp.scala:

Code: Alles auswählen

def cycBindAndInterp(boundId: Symbol, namedExpr: RCFLAE, env: Env): Env = {
    lazy val recEnv: Env = (id: Symbol) => {
        if (id == boundId) interp(namedExpr, recEnv)
        else env(id)
    }
    recEnv
}
But is this actually needed? Scala does not directly evaluate functions when you pass them around. So I guess using def should actually work, too.

mirko-koehler
Mausschubser
Mausschubser
Beiträge: 66
Registriert: 16. Okt 2017 12:28

Re: Environments as Scala Functions: Problem with recBind

Beitrag von mirko-koehler » 2. Mär 2018 17:59

I don't know what was said in the lecture, and maybe I'm missing something, but the code works with def and with lazy val. With val, however, it does not work because you refer to recEnv in the definition of recEnv.

Antworten

Zurück zu „Archiv“