1
let x = ref 100 in
let f () = !x in
let x = ref 50 in
??? ; f ()

You are supposed to get the answer to be 50 by plugging something into the ??? (not shadowing). But I don't know how to change the value of your original ref now, considering x := 50 is going to apply to the SECOND x now....

Paul
  • 375
  • 1
  • 5
  • 17

2 Answers2

4

If it really is a "beginner question", that's probably not the expected answer. But I don't know what the "expected answer" is, so let's hack around instead.

Obj.(obj (field (repr f) 1)) := 42;;

relevant link

gasche
  • 31,259
  • 3
  • 78
  • 100
1

There is no way to access original x now. So.. shadow f!

ygrek
  • 6,656
  • 22
  • 29
  • It's a problem I'm doing =/ I'm not allowed to shadow. It was from last years final; the solution shows shadowing, but they didn't intend on allowing people to shadow so they wrote on this practice one "DO NOT USE LET" -- So there must be a way to get f () to equal 42 without shadowing! – Paul May 13 '11 at 16:00
  • 3
    @Paul If the sentence is "DO NOT USE LET", it's easy: `match fun () -> 42 with f -> () ; f () ;;` – Pascal Cuoq May 13 '11 at 23:00
  • I did not realize you can match on a function. Other than this contrived problem, is there a useful case for this? I don't see how since there are no function patterns. – Ashish Agarwal May 17 '11 at 17:47