Fibonacci with ranges
Russel Winder
russel at russel.org.uk
Sat Mar 12 02:37:19 PST 2011
On Sat, 2011-03-12 at 02:15 -0800, Ali Çehreli wrote:
[ . . . ]
> void main()
> {
> long[] data = [ 0, 1, 1, 2, 3, 5, 8 ];
>
> foreach (n; 0 .. data.length) {
> assert(equal(declarative(n), data[0..n]));
> }
> }
In fact the driver is:
unittest {
immutable data = [
[ 0 , 0 ] ,
[ 1 , 1 ] ,
[ 2 , 1 ] ,
[ 3 , 2 ] ,
[ 4 , 3 ] ,
[ 5 , 5 ] ,
[ 6 , 8 ] ,
[ 7 , 13 ] ,
[ 8 , 21 ] ,
[ 9 , 34 ] ,
[ 10 , 55 ] ,
[ 11 , 89 ] ,
[ 12 , 144 ] ,
[ 13 , 233 ] ,
] ;
foreach ( item ; data ) {
assert ( iterative ( item[0] ) == item[1] ) ;
}
foreach ( item ; data ) {
assert ( declarative ( item[0] ) == item[1] ) ;
}
}
so I need to index the take-n list to return the last value.
This of course brings up the question of the signature of any factorial
function. Without a real use case it is a rhetorical question. What is
nice though is that there could be a neat way of generating a memoized,
i.e. cached, lazy list.
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder at ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at russel.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110312/9192e499/attachment.pgp>
More information about the Digitalmars-d-learn
mailing list