Re: [stack] sweetening concatenative syntax
Stevan Apter <[email protected]>
| Newsgroups | gmane.comp.lang.concatenative |
|---|---|
| Message-ID | <[email protected]> |
forget about the partial application case. what i want in my toolkit is a single combinator 'map' which takes an n-ary function and applies it to each of a list of length-n lists. not map1, map2, ... for each case. is that what's going on here? or does typing rule out having such a combinator? ----- Original Message ----- From: "John Nowak" <[email protected]> To: <[email protected]> Sent: Thursday, March 06, 2008 5:40 PM Subject: Re: [stack] sweetening concatenative syntax > > On Mar 6, 2008, at 5:00 PM, Stevan Apter wrote: > >> ack. it's been so long since i looked at this stuff that i'd >> forgotten that 'map' in f/g and XY is an extension of the standard >> definition: given a list of k-length lists, it applies a n-ary >> function n>=k to each element of the list. so e.g. >> >> [[1 2][3 4][5 6]][+]map -> [3 7 11] >> >> if n>k, then e.g. f "projects": >> >> [[1][3 4][5 6 7]][+ *][map] -> [[1 + *][7 *] 18] >> >> i knew it looked more complex than it had to be! anyway, i'd >> be curious how you would write this combinator (call it 'mapx'). > > (I'm only going to handle the first case of 'map' above. There's no > way for the type system to handle the second.) > > Here's one way, writing other highly useful combinators as we go: > > f map = [ ] [x xs -> x f i xs f map cons] unlist > z f foldl = [z] [x xs -> xs z x f i f foldl] unlist > foldl1 = [uncons swap] dip fold > mapx = [foldl1] compose map > > Here's another, this time pointfree. I stole the definition of 'foldl' > from Cat; there may be a better one. The 'pa' function is partial > application (a [B a -> C] -> [B -> C]). Note that this version of map > is tail recursive, unlike the above version: > > foldl = swapd [dip] pa [uncons swap] swap compose [null? not] while > foldl1 = [uncons swap] dip fold > rmap = null swap [cons] compose fold > map = rmap reverse > mapx = [foldl1] compose map > > - John >