bug-apl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

alternate fibonacci sequence using recursive list traversal


From: Rowan Cannaday
Subject: alternate fibonacci sequence using recursive list traversal
Date: Wed, 20 Nov 2019 12:40:25 -0500

This topic has been discussed before, but here is a useful function that can be used to recursively traverse a list. This might be helpful when you need to seed an algorithm with a initial value, that cascades through the operation. The fibonacci performance seems to be quite good.

list ← {↑{⍺ ⍵}/⍵,'∘'}
∇Z ← A (L ltrav) B ;h;t
    Z ← A ⍝ set output to accm before branch
    →(B≡'∘')/0 ⍝ guard cond
    (h t) ← B ⍝ head/tail
    Z ← (A L h) (L ltrav) t ⍝ recursively call self w/ accumulated head and tail


0 ({⍺+1} ltrav) list 1 2 3
⍝ fibonacci seq
1 ({⍺,(¯1↓¯2↑⍺)+¯1↑⍺} ltrav) list ⍳10

Cheers,

Rowan Cannaday

reply via email to

[Prev in Thread] Current Thread [Next in Thread]