Skip to content
This repository has been archived by the owner on Oct 23, 2018. It is now read-only.

Chapter 8.17 - Mutable State: Modifications required to simulate and simulate' functions #114

Open
sevanspowell opened this issue Oct 21, 2017 · 0 comments

Comments

@sevanspowell
Copy link

Chapter 8.17 - Mutable State

For code to build, need to explicitly discard result of modifySTRef in the simulate function. Behaviour introduced in purescript/purescript#1803 (I think).

That is, need to change this:

simulate :: forall eff h. Number -> Number -> Int -> Eff (st :: ST h | eff) Number
simulate x0 v0 time = do
  ref <- newSTRef { x: x0, v: v0 }
  forE 0 (time * 1000) \_ -> do
    modifySTRef ref \o ->
      { v: o.v - 9.81 * 0.001
      , x: o.x + o.v * 0.001
      }
    pure unit
  final <- readSTRef ref
  pure final.x

to this:

...
    _ <- modifySTRef ref \o ->
...

Also the simulate' function would not build because the type signatures of simulate and simulate' don't match:

simulate :: forall eff h. Number -> Number -> Int -> Eff (st :: ST h | eff) Number
simulate' :: Number -> Number -> Number -> Number
simulate' x0 v0 time = runPure (runST (simulate x0 v0 time))

time is an Int in simulate and Number in simulate'. I needed to convert time to a Int using Data.Int.round:

import Data.Int (round)

...

simulate' :: Number -> Number -> Number -> Number
simulate' x0 v0 time = runPure (runST (simulate x0 v0 (round time)))

but there might be better ways to do this.

By the way Phil, I'm really enjoying this book and it's (challenging) exercises. Thanks heaps!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant