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 06:01 pm (UTC)mapi (fn (v,i) => [code]) list?(no subject)
Date: 2010-04-28 06:21 pm (UTC)is "fn" like "lambda" and "=>" like the "." following a lambda?
Your one line of code above doesn't enumerate.
(no subject)
Date: 2010-04-28 07:00 pm (UTC)it does enumerate. it applies the provided function (which takes the index as an argument) to every element of the list, and returns a list of the results. "appi" can be used if you just want to apply some effectful computation to every element, like printing it.
(no subject)
Date: 2010-04-29 04:25 am (UTC)You can abbreviate the writeup even further if you're single-national Canadian.
(no subject)
Date: 2010-04-29 04:39 am (UTC)but yeah i dunno, posting the actual implementation seems pedagogically more necessary in the ML example since a "map" call is totally bizarre-looking to someone used to seeing a loop structure.
(no subject)
Date: 2010-04-29 01:57 pm (UTC)mapi (fn (x, i) => printf I'" => "S $ i x) thelist, and you're worried they won't understand that line of code, why expect they'll get any illumination out of the implementation of mapi?This is a problem I've frequently had with CMU SML people. I'm used to a world that believes in abstraction. This method of giving the detailed implementation first is the very antithesis of abstraction.
(no subject)
Date: 2010-04-29 04:30 pm (UTC)(no subject)
Date: 2010-04-29 05:33 pm (UTC)(no subject)
Date: 2010-04-30 04:57 am (UTC)(no subject)
Date: 2010-04-29 04:41 am (UTC)(no subject)
Date: 2010-04-29 04:55 am (UTC)Here's a very short solution in another language you probably don't know:
Maybe Haskell is inherently more readable, though? :P This one makes creative use of laziness!
(no subject)
Date: 2010-04-29 05:10 am (UTC)(no subject)
Date: 2010-04-29 05:20 am (UTC)(no subject)
Date: 2010-04-29 05:19 am (UTC)mapi (fn (i, el) => ... code(i, el) ...) listis less elegant than
because
* the Python fewer tokens
* in the ML, what the heck is "..." anyway?
* in the ML, one explicitly writes the lambda. One of the reasons Paul Graham invented Arc is because "lambda(x)" appears too often in Lisp.
(no subject)
Date: 2010-04-29 01:32 pm (UTC)(no subject)
Date: 2010-04-29 01:40 pm (UTC)(no subject)
Date: 2010-04-29 01:44 pm (UTC)