We are having the hardest time simply understanding how to implement
this final lab in java...
Basically, we thought of doing a first pass in which we would store
the function names and their body (the expression) in a Hashmap
signature. Once that is done we would execute the main function
(provided it has been declared, otherwise there would be an error).
How problem here is, if we were given a simple program such as:
f x y = x + y;
main = f 5 7
Our interpreter will return the value of the expression "f 5 7". It
sees it as a function application (EApp in our grammar). We get the
function's body from the signature Hashmap... but we can't see how to
apply that body on the list of variables/integers in input!
Can anybody hint us on this?
Thanks in advance
It's important the way you store the function declarations. The idea
would be not to store f as x + y but as something like \x -> \y -> x +
y because the order of the arguments matters.
When you apply f to 4 and 5, first you treat the case of applying f to
4 (since function application is left-associative). You can either
substitute x with 4 in the body of the abstraction (substitution-based
approach) or work with closures, as described in the extra lecture. If
you work with substitutions, you might make sure that you don't get
variable capture.
For example (\x -> \x -> x) 1 2 should yield to 2, if the substitution
is performed correctly.
I hope that helps.
Regards,
Ramona
2010/3/10 Alexis Vanacker <alexva...@gmail.com>:
(it didn't work anyway when we tried it out).
On 10 mar, 15:01, Ramona Enache <ra.moni...@gmail.com> wrote:
> Hi,
>
> It's important the way you store the function declarations. The idea
> would be not to store f as x + y but as something like \x -> \y -> x +
> y because the order of the arguments matters.
> When you apply f to 4 and 5, first you treat the case of applying f to
> 4 (since function application is left-associative). You can either
> substitute x with 4 in the body of the abstraction (substitution-based
> approach) or work with closures, as described in the extra lecture. If
> you work with substitutions, you might make sure that you don't get
> variable capture.
> For example (\x -> \x -> x) 1 2 should yield to 2, if the substitution
> is performed correctly.
> I hope that helps.
>
> Regards,
> Ramona
>
> 2010/3/10 Alexis Vanacker <alexvanac...@gmail.com>: