-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ch8 restore Exceptions and Mutable State sections #94
Ch8 restore Exceptions and Mutable State sections #94
Conversation
The Mutable State section might need another update if Edit, summary of ways to track mutable state:
|
text/chapter8.md
Outdated
var simulate = function (x0) { | ||
return function (v0) { | ||
return function (time) { | ||
return (function __do() { | ||
var ref = { | ||
value: { | ||
x: x0, | ||
v: v0 | ||
} | ||
}; | ||
Control_Monad_ST_Internal["for"](0)(time * 1000 | 0)(function (v) { | ||
return Control_Monad_ST_Internal.modify(function (o) { | ||
return { | ||
v: o.v - 9.81 * 1.0e-3, | ||
x: o.x + o.v * 1.0e-3 | ||
}; | ||
})(ref); | ||
})(); | ||
return ref.value.x; | ||
})(); | ||
}; | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not as concise as is was in the original.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the new version have a call to Control_Monad_ST_Internal.modify
?
This is the original body:
var ref = { x: x0, v: v0 };
Control_Monad_Eff.forE(0)(time * 1000 | 0)(function (i) {
return function __do() {
ref = (function (o) {
return {
v: o.v - 9.81 * 1.0e-3,
x: o.x + o.v * 1.0e-3
};
})(ref);
return Prelude.unit;
};
})();
return ref.x;
This is the new body:
var ref = { value: { x: x0, v: v0 } };
Control_Monad_ST_Internal["for"](0)(time * 1000 | 0)(function (v) {
return Control_Monad_ST_Internal.modify(function (o) {
return {
v: o.v - 9.81 * 1.0e-3,
x: o.x + o.v * 1.0e-3
};
})(ref);
})();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this probably does indicate a problem. There is an inliner rule for modify
built in to the compiler but it looks like it is no longer firing for some reason.
text/chapter8.md
Outdated
## Exercises | ||
|
||
1. (Medium) Rewrite the `safeDivide` function to throw an exception using `throwException` if the denominator is zero. | ||
1. (Difficult) The following is a simple way to estimate pi: randomly choose a large number `N` of points in the unit square, and count the number `n` which lie in the inscribed circle. An estimate for pi is `4n/N`. Use the `random` and `ST` effects with the `for` function to write a function which estimates pi in this way. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it still possible to blend Effect.random
with ST
? This is one of the more interesting exercises, so I'd like to keep it. Maybe it should be moved to ch11 in the "State Monad" section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Once you're in Effect
you might as well use Ref
. When we were still using Eff
you could eliminate the ST
effect, but that was always kind of dodgy"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the second exercise can't include randomness, then it seems like it could actually be tackled in ch4 like so:
estimatePi :: Int -> Number
estimatePi r =
let
bigN = r * r
n =
sum do
x <- 1 .. r
y <- 1 .. r
guard $ x * x + y * y < r * r
pure 1
in
(toNumber (4 * n)) / (toNumber bigN)
I'm not sure if adding this to ch4 would be an improvement, since it doesn't test any new language concepts.
There's still the question of what exercise to use for ST
in this chapter.
cbc5fa2
to
7880ed5
Compare
Original text here:
https://leanpub.com/purescript/read#leanpub-auto-handlers-and-actions
Split this into three commits to make changes easier to follow.