Paste: recurrence generator

Author: rswarbrick
Mode: factor
Date: Tue, 15 Sep 2009 22:50:25
Plain Text |
USING: kernel sequences math locals ;

: mid-depth-clone ( seq -- seq' )
    [ clone ] map ;

:: [recur-quot] ( vals quot -- quot' )
    [
        vals first2 quot call
        vals second 0 vals set-nth 1 vals set-nth
    ] ; inline

! quots should be ( a[n-1] a[n] -- a[n+1] )
:: 2-recurrence ( n start-vals quots -- end-vals )
    start-vals mid-depth-clone
    dup quots [ [recur-quot] ] 2map [ n ] dip
    [ over 0 > ]
    [ [ 1 - ] [ dup [ call ] each ] bi* ] while 2drop ; inline

! Use like
!    10 { { 1 1 } } { [ + ] } 2-recurrence .

Annotation: take 2

Author: rswarbrick
Mode: factor
Date: Tue, 15 Sep 2009 23:10:20
Plain Text |
:: [recur-quot] ( vals quot -- quot' )
    [
        vals first2 quot call
        vals second vals set-first vals set-second
    ] ;

! quot should be ( a[n-1] a[n] -- a[n+1] )
:: 2-recurrence ( n start-vals quot -- end-vals )
    start-vals clone dup quot [recur-quot] [ n ] dip
    [ over 0 > ]
    [ [ 1 - ] [ dup call( -- ) ] bi* ] while 2drop ;

Annotation: Take 3

Author: rswarbrick
Mode: factor
Date: Tue, 15 Sep 2009 23:26:59
Plain Text |
! *sigh* So this is somewhat simpler...

: 2-recurrence ( n start-vals quot -- a[n] )
    [ first2 rot ] dip [ tuck ] prepose times nip ; inline

New Annotation

Summary:
Author:
Mode:
Body: