问题描述
大家好,
我没有经历过函数式编程,但是现在我想要学习Haskell并且我已经学到了:1)功能性
编程LISTS是基础; 2)任何循环。在FP中变成
递归。
我也知道Python有一些有用的工具,比如map,filter,
reduce ...所以我告诉:让我们尝试使用
Python进行一些FP风格的编程!我拿了一个Haskell的例子:
listprimes ::整数 - [整数]
listprimes n =如果n == 0则筛选[2 .. ]否则筛[2 ..(n-1)]
其中
筛[] = []
筛(p:xs) = p:筛子(过滤器(\ x -mod xp 0)xs)
我尝试翻译它在Python中:
def sieve(s):
如果s == []:
返回[]
else:
返回[s [0]] +筛子(过滤器((lambda x:x%s [0] 0),
s [ 1:]))
def listprimes(n):
返回筛(范围(2,n))
这些应该几乎相同:listprimes实际上列出了最多n-1的素数
整数。结果是:Haskell实现运行良好,
也许它不是更好的方法,但它做了我想要的。
Python实现给了我
RuntimeError:cmp中超出的最大递归深度
我的问题是:我们如何调用语言功能?如果它是主要的
实现有限的堆栈?或者我的代码错了吗?
LS
Hi all,
I haven''t experienced functional programming very much, but now I''m
trying to learn Haskell and I''ve learned that: 1) in functional
programming LISTS are fundmental; 2) any "cycle" in FP become
recursion.
I also know that Python got some useful tool such as map, filter,
reduce... so I told: "let''s try some FP-style programming with
Python!". I took a little example of Haskell:
listprimes :: Integer -[Integer]
listprimes n = if n == 0 then sieve [2..] else sieve [2..(n-1)]
where
sieve [] = []
sieve (p:xs) = p : sieve (filter (\x -mod x p 0) xs)
and I tried to "translate" it in Python:
def sieve(s):
if s == []:
return []
else:
return [s[0]] + sieve(filter((lambda x: x % s[0] 0),
s[1:]))
def listprimes(n):
return sieve(range(2,n))
These should be almost the same: listprimes actually lists prime
integers up to n-1. The result is: Haskell implementation works well,
maybe it''s not the better way to do it, but it does what I wanted.
Python implementation gives me
RuntimeError: maximum recursion depth exceeded in cmp
My question is: how can we call a language "functional" if it''s major
implementation has a limited stack? Or is my code wrong?
LS
推荐答案
Python不优化尾递归。您可以使用sys.setrecursionlimit增加最大
递归限制,但代码仍然是
慢。
我是函数式编程语言的粉丝(包括Haskell!),
但是我不会尝试用Python编写函数代码 - 语言
没有优化对于这种类型的代码,以及它提供的语法
与其他函数语言相比并不是很优雅。如果您想要编写功能代码,请使用真正的函数式语言!
-
Evan Klitzke< ev * *@yelp.com>
Python does not optimize tail recursion. You can increase the maximum
recursion limit with sys.setrecursionlimit, but the code will still be
slow.
I am a fan of functional programming languages (including Haskell!),
but I wouldn''t try to write functional code in Python -- the language
isn''t optimized for this type of code, and the syntax it provides
isn''t very elegant, compared to other functional languages. If you
want to write functional code, use a real functional language!
--
Evan Klitzke <ev**@yelp.com>
[2,3,5] ,7,11,13,17,19,23,29]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
我试着写一个更短的生成器表达式筛子但是不能
搞定它。
有人可以帮忙吗?下面是非工作代码
def si(l):
p = l.next()
产量p
(如果x%p!= 0,则x为si(l)中的x)
某处应该有收益或收益但是无法弄清楚
2007年9月18日,Lorenzo Stella< lo ****** @ gmail.comwrote:
I tried to write a shorter generator expression based sieve but cant
get it right.
Can someone help? Heres the non-working code
def si(l):
p = l.next()
yield p
(x for x in si(l) if x % p != 0)
There should be an yield or return somewhere but cant figure it out
On 9/18/07, Lorenzo Stella <lo******@gmail.comwrote:
将最后一行改为
为x in(如果x%p!= 0则x为si(l)中的x) :产量x
如果你愿意的话。
Alex
Change last line to
for x in (x for x in si(l) if x % p != 0): yield x
if you wish.
Alex
这篇关于Python可以作为功能有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!