## iterate.pkg
# Compiled by:
#
src/lib/std/standard.lib### "Using Java for serious jobs is
### like trying to take the skin off a
### rice pudding wearing boxing gloves."
###
### -- Tel Hudson
package iterate: Iterate {
fun bad_arg (f, msg)
=
lib_base::failure { module=>"iterate", fn=>f, msg };
fun iterate f count init
=
{ fun iter (0, v) => v;
iter (n, v) => iter (n - 1, f v);
end;
count < 0
##
?? bad_arg ("iterate", "count < 0")
:: iter (count, init);
};
fun repeat f count init
=
{ fun iter (n, v)
=
n == count
##
?? v
:: iter (n+1, f (n, v));
count < 0
##
?? bad_arg ("repeat", "count < 0")
:: iter (0, init);
};
fun forloop f (start, stop, inc)
=
{ fun up (n, v) = if (n > stop) v; else up (n+inc, f (n, v)); fi;
fun down (n, v) = if (n < stop) v; else down (n+inc, f (n, v)); fi;
if (start < stop)
#
if (inc <= 0) bad_arg ("forloop", "inc <= 0 with start < stop");
else \\ v = up (start, v);
fi;
elif (stop < start)
if (inc >= 0)
#
bad_arg ("forloop", "inc >= 0 with start > stop");
else
\\ v = down (start, v);
fi;
else
\\ v = f (start, v);
fi;
};
}; # iterate
## COPYRIGHT (c) 1993 by AT&T Bell Laboratories. See SMLNJ-COPYRIGHT file for details.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.