Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > sci.math > #641173

Re: C Question for P. Olcott

Message-ID <2o71MB3oJ3SYPilCF3_oo269lD0@jntp> (permalink)
Subject Re: C Question for P. Olcott
References <eQyZF6cs5VUuRpMR1mqnFr_hTQQ@jntp>
Newsgroups sci.math
Date 2025-11-26 03:50 +0000
Organization Nemoweb
From Python <python@cccp.invalid>

Show all headers | View raw


Moreover, Peter, do you understand why this code works:

#!/usr/bin/env python3

# Combinateur de point fixe (Y adapté à Python, appel par valeur)
Y = lambda f: (lambda x: f(lambda *args: x(x)(*args)))(
                  lambda x: f(lambda *args: x(x)(*args))
              )

# Exemple 1 : factorielle sans récursion directe
fact = Y(
    lambda rec: lambda n: 1 if n == 0 else n * rec(n - 1)
)

# Exemple 2 : Fibonacci sans récursion directe
fib = Y(
    lambda rec: lambda n: n if n < 2 else rec(n - 1) + rec(n - 2)
)

if __name__ == "__main__":
    for n in range(6):
        print(f"fact({n}) =", fact(n))

    for n in range(8):
        print(f"fib({n})  =", fib(n))

Back to sci.math | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

C Question for P. Olcott Python <python@cccp.invalid> - 2025-11-26 03:02 +0000
  Re: C Question for P. Olcott Python <python@cccp.invalid> - 2025-11-26 03:50 +0000

csiph-web