hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

Structural hashing changed in 1.9.6.16?

Last post 07-02-2009, 0:10 by A.deWinkel. 3 replies.
Sort Posts: Previous Next
  •  06-30-2009, 5:54 11147

    Structural hashing changed in 1.9.6.16?

    The following code always served me well under 1.9.6.2:

    open System.Collections.Generic
    let Cached f =
      let cache = new Dictionary<_,_>()
      let inline set a r = cache.[ a ] <- r; r
      fun a ->
        match cache.TryGetValue a with
        | true, result -> result
        | _ -> f a |> set a

    let Cached2 f =
      fun a b -> Cached (fun (a,b) -> f a b) (a,b)

    let sq =
      let sq x =
        printfn "%d * %d" x x
        x * x
        Cached sq

    sq 3 |> printfn "%d"
    sq 3 |> printfn "%d"

    let plus =
      let plus x y =
        printfn "%d + %d" x y
        x + y
        Cached2 plus

    plus 1 3 |> printfn "%d"
    plus 1 3 |> printfn "%d"

    Cached still works fine in 1.9.6.16, but Cached2 doesn't. I suppose it relies on structural hashing. Has something fundamentally changed here? It definitely broke my code! Any suggestions how to fix this?

    Wiebe

  •  07-01-2009, 14:26 11165 in reply to 11147

    Re: Structural hashing changed in 1.9.6.16?

    The following does the trick:

    let Cached2 f =
      let c = Cached (fun (a, b) -> f a b)
      fun a b -> c (a,b)


  •  07-01-2009, 23:03 11169 in reply to 11165

    Re: Structural hashing changed in 1.9.6.16?

    Thanks!

    Makes sense, strange it worked differently before.

  •  07-02-2009, 0:10 11171 in reply to 11169

    Re: Structural hashing changed in 1.9.6.16?

    funny, but what does this caching mean?
    (I needed to intend the Cached sq and Cached2 plus under the let (so one indentation left))
    same thing as outputted:
    let sqr x =
    printfn "%d * %d" x x
    x * x
    sqr 3 |> printfn "%d"
    plus x y =
    printfn "%d + %d" x y
    x + y
    plus 1 3 |> printfn "%d"

    thought letting me peek into the outer let's however also:
    let sqr =
    let sqr x =
    printfn "%d * %d" x x
    x * x
    sqr
    sqr 3 |> printfn "%d"
    let plust =
    let plust x y =
    printfn "%d + %d" x y
    x + y
    plust
    plust 1 3 |> printfn "%d"

    works like the cached version. (didn't know yet that you can make a call directly to a let defined inside an outer let !?)
    don't mind just trying to understand F#, I reckon the reason to Cache things aren't shown here, and you'll retrieve it someplace else afterwards.

    (I moved to IE8.0 yesterday, for some reason this gives a different interface, not allowing me to keep the intended indentation in the above)
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems