What is the "functional" way of doing this?

Paul Rubin http
Tue Jul 31 01:28:44 CEST 2007


beginner <zyzhu2000 at gmail.com> writes:
> def f(n):
>      l=[]
>      while n>0:
>          l.append(n%26)
>          n /=26
>     return l
> 
> I am wondering what is the 'functional' way to do the same.

If you're trying to learn functional programming, maybe you should use
a functional language like Haskell or Scheme.  But anyway you might be
thinking of something like:

def f(n):
   def mseq(n):
      while n > 0:
         n,a = divmod(n, 26)
         yield a
   return list(mseq(n))

The above is not really "functional", but it's a reasonably natural
Python style, at least for me.



More information about the Python-list mailing list