Re: [stack] sweetening concatenative syntax
John Nowak <[email protected]>
| Newsgroups | gmane.comp.lang.concatenative |
|---|---|
| Message-ID | <[email protected]> |
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