什么是“功能性”这样做的方式? [英] What is the "functional" way of doing this?

查看:102
本文介绍了什么是“功能性”这样做的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果我有一个数字n并想要生成一个基于类似

的列表:

def f(n):

l = []

而n> 0:

l.append(n%26 )

n / = 26

返回l


我想知道做什么的功能方法是什么。


谢谢,

初学者

解决方案

初学者< zy ******* @ gmail.comwrites:


def f(n):

l = []

而n> 0:

l.append(n%26)

n / = 26

返回l


我想知道什么是功能的方式来做同样的事情。



如果您正在尝试学习函数式编程,也许您应该使用

这样的函数式语言,如Haskell或Scheme。但无论如何你可能会想到这样的事情:


def f(n):

def mseq(n):

而n 0:

n,a = divmod(n,26)

产生一个

的返回列表( mseq(n))


以上并不是真正的功能性,但它是相当自然的,而且至少对我来说是蟒蛇风格。


7月30日下午3:48,初学者< zyzhu2 ... @ gmail.comwrote:





如果我有一个数字n并希望生成一个基于类似

的列表:


def f(n):

l = []

而n> 0:

l.append(n%26)

n / = 26

返回l


我想知道什么是功能的方式来做同样的事情。



递归在函数式编程中很常见:


def f(n,l =无):

如果l ==无:

l = []

如果n 0:

返回f(n / 26,l + [n%26])

其他:

返回l


打印f(1000)


-

希望这会有所帮助,

史蒂文


2007年7月30日星期一22:48:10 +0000,初学者写道:





如果我有一个数字n并且想要生成一个基于的列表比如

以下:


def f(n):

l = []

while n> 0:

l.append(n%26)

n / = 26

返回l


我想知道做什么的功能方法是什么。



对我来说似乎是一个非常好的功能:)

我不知道功能性,但是那个''急于写成

发电机:


def f(n):

而n 0:

n,x = divmod(n,26)

收益率x


并且在使用中:


>> for f in f(1000):



....打印x

....

12

12

1


>> list(f(1000))



[12,12,1]

-

Steven。


Hi,

If I have a number n and want to generate a list based on like the
following:

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.

Thanks,
beginner

解决方案

beginner <zy*******@gmail.comwrites:

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.


On Jul 30, 3:48 pm, beginner <zyzhu2...@gmail.comwrote:

Hi,

If I have a number n and want to generate a list based on like the
following:

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.


Recursion is common in functional programming:

def f(n, l=None):
if l == None:
l = []
if n 0:
return f(n/26, l + [n%26])
else:
return l

print f(1000)

--
Hope this helps,
Steven


On Mon, 30 Jul 2007 22:48:10 +0000, beginner wrote:

Hi,

If I have a number n and want to generate a list based on like the
following:

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.


Seems like a perfectly good function to me :)
I don''t know about "functional", but that''s crying out to be written as a
generator:

def f(n):
while n 0:
n, x = divmod(n, 26)
yield x

And in use:

>>for x in f(1000):

.... print x
....
12
12
1

>>list(f(1000))

[12, 12, 1]
--
Steven.


这篇关于什么是“功能性”这样做的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆