loops: having my cake and eating it too?
Apr. 27th, 2010 07:43 pmOne programming annoyance when writing loops is wanting to access each element of the list without passing the index, i.e. with code like:
I'm not aware of any solutions to this in current usage.
for (el in list) while simultaneously wanting to know the index without writing i=0 and i++.I'm not aware of any solutions to this in current usage.
(no subject)
Date: 2010-04-28 03:10 am (UTC)local fun mapi_aux i f [] = [] | mapi_aux i f (x::xs) = f (i, x) :: mapi_aux (i+1) f xs in (* mapi : (int * 'a -> 'b) -> 'a list -> 'b list *) fun mapi f list = mapi_aux 0 f list endNow instead of
for (el in list) { ... code(i, el) ... }you just saymapi (fn (i, el) => ... code(i, el) ...) list.The Standard ML Basis has things like
Array.iteriandVector.mapipredefined:(no subject)
Date: 2010-04-28 03:16 am (UTC)