
Hi folks, I think I need a monad but I'm not sure which, and maybe something simpler would do. I want to model a fish tank as a function of time onto how many fish there are in it at that time. If I already have such a function, I want to operate on it with something like "add 2 fish on day 20" or "take 3 away on day 15" to get a new function of the same form, but the latter should not remove more fish than there are in the tank at that time, and it should tell me how many I get. I don't promise to apply these operators in chronological order. This seems like the kind of thing that would be in the prelude somewhere. But where? TIA, Adrian.

From the description, it sounds like you need an "evaluation procedure" more than you need a monad (though a solution is likely to use a state monad to track the number of fish in the tank).
How would you want to account for days? E.g. Do you want to run for a month (dropping actions outside the time frame) or do you want to run until the last action occurs?

Adrian May
I want to model a fish tank as a function of time onto how many fish there are in it at that time. If I already have such a function, I want to operate on it with something like "add 2 fish on day 20" or "take 3 away on day 15" to get a new function of the same form, but the latter should not remove more fish than there are in the tank at that time, and it should tell me how many I get. I don't promise to apply these operators in chronological order.
This seems like the kind of thing that would be in the prelude somewhere. But where?
Let's see. You want to model a "number of fish" value: Integer but that value depends on time: Time -> Integer So you have a "what do I get?" and a "how do I get it?". The former can be abstracted away: type Timed a = Time -> a type Timed a = (->) Time a type Timed = (->) Time And yes, Timed is a monad. You may know it as Reader Time, but Reader is just (->) in disguise. However, you don't need it to be a monad: applyAt :: Time -> (a -> a) -> Timed a -> Timed a applyAt tEv f c t | t >= tEv = f (c t) | otherwise = c t Then you can add two fish on day 20: applyAt 20 (+ 2) and take 3 away on day 15: applyAt 15 (subtract 3) Have fun. =) Greets, Ertugrul -- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.

Thanks! In the mean time I figured out that I need something completely
different though. Actually, they're not fish, they're assorted programmers,
and I need to approach the hiring plan with tasks (as in the Gantt chart
code at http://nuerd.blogspot.com) calling for certain skills and get a
possibly non-contiguous booking. A Timed Integer won't support that though,
unless I step through it day by day which would seem needlessly goofy. It's
at brainstorming stage right now but I think I can probably plough through
it, then I'll probably be back asking for help with boilerplate reduction.
Adrian.
On 18 May 2013 14:48, "Ertugrul Söylemez"
Adrian May
wrote: I want to model a fish tank as a function of time onto how many fish there are in it at that time. If I already have such a function, I want to operate on it with something like "add 2 fish on day 20" or "take 3 away on day 15" to get a new function of the same form, but the latter should not remove more fish than there are in the tank at that time, and it should tell me how many I get. I don't promise to apply these operators in chronological order.
This seems like the kind of thing that would be in the prelude somewhere. But where?
Let's see. You want to model a "number of fish" value:
Integer
but that value depends on time:
Time -> Integer
So you have a "what do I get?" and a "how do I get it?". The former can be abstracted away:
type Timed a = Time -> a type Timed a = (->) Time a type Timed = (->) Time
And yes, Timed is a monad. You may know it as Reader Time, but Reader is just (->) in disguise. However, you don't need it to be a monad:
applyAt :: Time -> (a -> a) -> Timed a -> Timed a applyAt tEv f c t | t >= tEv = f (c t) | otherwise = c t
Then you can add two fish on day 20:
applyAt 20 (+ 2)
and take 3 away on day 15:
applyAt 15 (subtract 3)
Have fun. =)
Greets, Ertugrul
-- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Adrian May
-
Ertugrul Söylemez
-
Stephen Tetley