Assignment 08 - Set semantic

Tai
Mausschubser
Mausschubser
Beiträge: 52
Registriert: 6. Feb 2012 23:36

Assignment 08 - Set semantic

Beitrag von Tai » 7. Jun 2015 19:37

Hello,

I have a question regarding the set(id,e) function.
I'm not able to complete scenario 4 of the provided tests. I have a problem with this section

Code: Alles auswählen

Let('driver, New('Driver), Seqn(
        Call('driver, 'drive, 'bike),
        Set('driver, New('LongDriver)),
        Call('driver, 'drive, 'bike))),
      Call('bike, 'speed))
I would like to know what set(id, e) should actually do.
I was thinking it should be something like this:
  • current program is in a class (checking via env('this)) => field name in the class exists => replace the fieldValue
  • not in a class || no field name exists => check for env(id) and update the value
I'am correct with these assumptions?

Thanks and cheers
T

Talaron
Mausschubser
Mausschubser
Beiträge: 85
Registriert: 26. Apr 2012 11:34

Re: Assignment 08 - Set semantic

Beitrag von Talaron » 8. Jun 2015 01:27

Actually, the semantics should be the opposite way: If an id is bound in the local environment, this binding should have preference. Only if the id would otherwise be unbound, it should be checked if there is a field matching the name.

This is by the way the same behavior that most object-oriented languages have. Consider the common style of setters in Java:

Code: Alles auswählen

class Example {
  private int x = 0;

  public void setX(int x) {
    this.x = x;
  }
}
Here, the the parameter name shadows the field, which can only be accessed by using the explicit get-field syntax "this.x".

Tai
Mausschubser
Mausschubser
Beiträge: 52
Registriert: 6. Feb 2012 23:36

Re: Assignment 08 - Set semantic

Beitrag von Tai » 9. Jun 2015 12:26

Well, that was kind of a stupid question from me :D Thanks for the clearness.

Now I have another question. What is the cleanest way to update a 'local' variable in the environment|store?
Since we can't change the environment in the Set(id,e) function, we have to update the store at the position where the local variable is currently stored.

To interpret the expression, we have to interpret it and get the position in the store of the interpreted expression.

To get the interpreted expression, we have to get it from the store and after that update the local variable in the store.
That looks something like "store.update(posOfId,store(posOfExpr))" with some more additional handlers. Is there a better way to do it? It looks quite complicated in the code and produces some overhead + uses unnecessary space in the store.

Cheers
T

Talaron
Mausschubser
Mausschubser
Beiträge: 85
Registriert: 26. Apr 2012 11:34

Re: Assignment 08 - Set semantic

Beitrag von Talaron » 9. Jun 2015 12:39

The way you described sounds correct. If you want your code to be more readable, remember that you can split it into separate steps and use vals for intermediate results. The sample solution uses five steps which are all quite simple.

Antworten

Zurück zu „Archiv“