return (1, a);
}
-//note, ma is edited by reference!
fmap(f, ma) :: (a -> b) -> (Int, a) -> (Int, b) {
+ var t = ma.snd; //copy to avoid editing ma by reference
if (ma.fst == 0) { return ma; }
else {
- ma.snd = f(ma.snd);
- return ma;
+ t = f(ma.snd);
+ return (ma.fst, t);
}
}
+
bind(ma, f) :: (Int, a) -> (a -> (Int, b)) -> (Int, b) {
if (ma.fst == 0) { return ma; }
else {
- return f(ma.snd);
+ return f(ma.snd);
}
}
main() {
var j = pure(True);
- var f = \b->!b;
var n = (0, False); //ugly that we need the additional parameter
var jf = fmap(\b->!b, j);
var fma = \b -> pure(!b);
- var bjf = bind(j, fma); //note is true because ma is passed by reference
+ var bjf = bind(j, fma);
bar bnf = bind(n, fma);
print("fmap Just false: ", jf.fst, jf.snd, "\n");
print("fmap Nothing: ", nf.fst, "\n");
- print("bind Just True: ", bjf.fst, bjf.snd, "\n");
+ print("bind Just False: ", bjf.fst, bjf.snd, "\n");
print("bind Nothing: ", bnf.fst, "\n");
}
\ No newline at end of file