On 2021-08-06 at 21:57:47 +1000, Steven D'Aprano <steve@pearwood.info> wrote:
On Thu, Aug 05, 2021 at 09:39:44AM +0100, Sam Frances wrote:
def fib(0): return 0
def fib(1): return 1
def fib(n): return fib(n-1) + fib(n-2)
I think that there is something rather disturbing about writing a function definition with a constant literal as parameter. It looks wrong and I'm sure it's going to confuse beginners.
You are not a beginner (nor am I); what's disturbing to you may or may not look wrong or confuse someone else. When I was a beginner, x = x + 1 was disturbing, wrong, and confusing to me. The proposed definition of the Fibonacci sequence mirrors the ones in Wikipedia¹ and OEIS,² and will certainly be at least familiar to those with a background in mathematics or coming to Python from functional languages. That said, I agree that it's not a good fit for Python, for reasons expressed elsewhere in this thread. FWIW, Lisp Flavored Erlang³ (a syntactically imperative language built atop the Erlang VM and run-time) doesn't repeat the "def," only the argument lists: lfe> (defun ackermann ((0 n) (+ n 1)) ((m 0) (ackermann (- m 1) 1)) ((m n) (ackermann (- m 1) (ackermann m (- n 1))))) Something like that might work in Python, but at that point, it's no different from a function that matches on its *args argument. ¹ https://en.wikipedia.org/wiki/Fibonacci_number ² https://oeis.org/A000045 ³ https://lfe.io/