can Python be useful as functional?

2007-09-17 Thread Lorenzo Stella
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can Python be useful as functional?

2007-09-18 Thread Lorenzo Stella
On 18 Set, 18:51, Grant Edwards <[EMAIL PROTECTED]> wrote:
> Perhaps Lorenzo Stella is referring to Python's lack of
> tail-recursion optimization?  There are languages that
> guarantee unlimited tail-recursion with a limited stack.

That's it.

Rustom Mody: your implementation lacks exactly where mine does. Try
listing the first 2000 primes... That's what I meant: I cannot in
general (with Python) get such list just by defining *what* it is, I
have to express *how* to get it (describing an algorithm).

"What" or "How": that is the question.

Steve Holden wrote:
> You just don't like the specific limit that Python imposes. So increase
> it with sys.setrecursionlimit().

That is obviously not the answer to my question.

-- 
http://mail.python.org/mailman/listinfo/python-list


https and POST method

2008-04-10 Thread Lorenzo Stella
Hi all,
I'm trying to write a simple script for sending sms via vyke... I have
to make a https
connection and pass some data with the POST method, like this perl
script does:

http://www.nutella.org/vyke.sms.txt

I tried to make the same, but it simply doesn't work! Any request
gives a 200 OK result... This is my code:

datah = {"act": "menulogin", "username": login, "password": passwd,
"menu_login_form": 1}
datas = urllib.urlencode(datah)
conn = httplib.HTTPSConnection("www.vyke.com")
conn.connect()
conn.request("POST", "/merchantsite/login.c?Distributor=MASKINA",
datas)
res = conn.getresponse()
print "login", res.status, res.reason
datah = {"act": "sendSMS", "from": numfrom, "to": numto, "message":
msg, "sms_form": 1}
datas = urllib.urlencode(datah)
conn.request("POST", "/merchantsite/sms.c", datas)
res = conn.getresponse()
print "send", res.status, res.reason
conn.request("GET", "/merchantsite/logout.c?Distributor=MASKINA")
res = conn.getresponse()
print "logout", res.status, res.reason
conn.close()

I don't know what to do! :-(
-- 
http://mail.python.org/mailman/listinfo/python-list