[Ex 7, Task 1] Exception in test of shadowing behaviour

Daniel S.
Mausschubser
Mausschubser
Beiträge: 87
Registriert: 25. Sep 2007 12:28
Wohnort: Mörfelden

[Ex 7, Task 1] Exception in test of shadowing behaviour

Beitrag von Daniel S. »

Hi, I get an exception in the following test case:

Code: Alles auswählen

(test (interp (with 'fact (num 42) (app fact (num 5)))) (num 120))
fun-body: expects argument of type <struct:fun>; given (num 42)
In my opinion, the exception is correct, because after substituting "fact", we get:

Code: Alles auswählen

(interp (app (num 42) (num 5)))
Which is a bad expression in the interpreter!?
Why shouldn't "fact" be substituted in this case (to get the expected value of 120)?
Mit freundlichen Grüßen
Daniel

tzeenie
Mausschubser
Mausschubser
Beiträge: 80
Registriert: 14. Okt 2008 20:04

Re: [Ex 7, Task 1] Exception in test of shadowing behaviour

Beitrag von tzeenie »

Daniel S. hat geschrieben:Why shouldn't "fact" be substituted in this case (to get the expected value of 120)?
Because the innermost definition should shadow the outer ones; in this case "fact" in the outer "with"-environment should be shadowed by the inner rec. It's a bit hard to see, because of the external definition of fact, but if you evaluate the test input to the interpreter, you get a better idea of what the interpreter actually "sees":

Code: Alles auswählen

> (with 'fact (num 42) (app fact (num 5)))
(with 'fact (num 42) (app (rec 'fact (fun 'n (if0 (id 'n) (num 1) (mult (id 'n) (app (id 'fact) (sub (id 'n) (num 1)))))) (id 'fact)) (num 5)))
--
p.s.: this is the most hideous language we've developed yet :lol:

Daniel S.
Mausschubser
Mausschubser
Beiträge: 87
Registriert: 25. Sep 2007 12:28
Wohnort: Mörfelden

Re: [Ex 7, Task 1] Exception in test of shadowing behaviour

Beitrag von Daniel S. »

Thanks for your hint and explanation, it's working now.
I forgot the definition of fact using "define", that also explains the missing ' before the second "fact" in this test case.
Mit freundlichen Grüßen
Daniel

Antworten

Zurück zu „Archiv“