[Tutor] learning recursion
Russel Winder
russel at winder.org.uk
Sun Feb 9 14:22:22 CET 2014
On Thu, 2014-02-06 at 18:06 -0500, Dave Angel wrote:
[…]
>
> Code:
> def fib2(n):
> if n==1:
> return 1
>
> elif n==2:
> return 1
> else:
> return fib2(n-2) +fib2(n-1)
[…]
I suggest it also be pointed out that this form is algorithmically
dreadful. Having transformed the maths to this first cut code, we then
need to consider it as code and shift to a tail recursive form,
iterative form, or at the very least memoize the function. Leaving
students of programming (in any current language) with the idea that
this is a good final solution is, I believe, a disservice.
--
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 winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
More information about the Tutor
mailing list