Author: | rswarbrick |
---|---|
Mode: | factor |
Date: | Thu, 10 Sep 2009 23:52:06 |
! The following *almost* works. Problem is that max-and-position doesn't ! have a static stack effect and I get a 'literal expected' error. ! ! Please could someone tell me what I'm doing wrong? ! ! I was hoping to have usage something like ! ! { 2 3 4 5 } [ sqmod ] max-and-position . ! ! => { 9 1 } ! ! Where ! : sqmod ( n -- n' ) sq 10 mod ; : 2choose ( x y x' y' ? -- x'' y'' ) [ [ 2drop ] 2dip ] [ 2drop ] if ; ! (x y) if x>x', else (x' y') : 2max ( x y x' y' -- x'' y'' ) [ 3dup nip < ] dip swap 2choose ; ! Result has effect ( prev elt ind -- next ), where ! prev={oldmax,oldind} and next={fun(elt),ind} or prev. : [max-position-fun] ( fun -- quot ) '[ [ [ first2 ] _ bi* ] dip 2max 2array ] ; : max-and-position ( seq fun -- max+pos ) [ dup length iota { -1 -1 } ] dip [max-position-fun] 2reduce ; inline