How do I call the double() function?

G

grocery_stocker

Given the following....

def double(val):
return val.bind(lambda x: val.return_(x*2))


I get "AttributeError: 'int' object has no attribute 'bind' " when I
try to do the following

double(2)

Below is the output...

[cdalten@localhost ~]$ python
Python 2.4.3 (#1, Oct 1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information..... return val.bind(lambda x: val.return_(x*2))
....Traceback (most recent call last):
File "<stdin>", line 1, in ?
 
J

John Machin

Given the following....

def double(val):
    return val.bind(lambda x: val.return_(x*2))

I get "AttributeError: 'int' object has no attribute 'bind' " when I
try to do the following

double(2)

Below is the output...

[cdalten@localhost ~]$ python
Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>> def double(val):

...     return val.bind(lambda x: val.return_(x*2))
...>>> double(2)

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in double
AttributeError: 'int' object has no attribute 'bind'

How you call it is irrelevant; you already achieved that.

The problem is that your double() function is weird. ... it fails
(quite justifiably) trying to execute 2.bind(). Just as well, because
the next problem would be 2.return_() ... int objects just don't have
such methods (as the error message pointed out).

Presumably you are trying to achieve a goal that's a bit more useful
than

def double(val):
return val * 2

so perhaps you could explain what that goal is and someone may be able
to help.
 
G

grocery_stocker

Given the following....
def double(val):
return val.bind(lambda x: val.return_(x*2))
I get "AttributeError: 'int' object has no attribute 'bind' " when I
try to do the following

Below is the output...
[cdalten@localhost ~]$ python
Python 2.4.3 (#1, Oct 1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>> def double(val):
... return val.bind(lambda x: val.return_(x*2))
...>>> double(2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in double
AttributeError: 'int' object has no attribute 'bind'

How you call it is irrelevant; you already achieved that.

The problem is that your double() function is weird. ... it fails
(quite justifiably) trying to execute 2.bind(). Just as well, because
the next problem would be 2.return_() ... int objects just don't have
such methods (as the error message pointed out).

Presumably you are trying to achieve a goal that's a bit more useful
than

def double(val):
return val * 2

so perhaps you could explain what that goal is and someone may be able
to help.


The function is taken from the article

"Understanding Monads Via Python List Comprehensions"

found at the following url

http://lukeplant.me.uk/blog.php?id=1107301643

Near the bottom of the article, the author has

"What is the <- in the do notation then? It is simply some syntactic
sugar that allows you to define the right kind of functions easily and
painlessly. It looks very much like 'unpack this data from the monad
so I can use it', so it helps conceptually. In fact, together with the
rest of the body of the 'do' block it forms an anonymous lambda
function, and we could write our double function something pretty much
like this in Python:

def double(val):
return val.bind(lambda x: val.return_(x*2))

(I've had to use return_ to avoid the clash with Python's 'return'
keyword). Haskell's do notation eliminates the explicit call to bind,
and the lambda, making it quite a bit easier to use. "

I just have no idea how to call double().
 
K

Kushal Kumaran

Given the following....

def double(val):
    return val.bind(lambda x: val.return_(x*2))

I get "AttributeError: 'int' object has no attribute 'bind' " when I
try to do the following

double(2)

<snipped>

See the usage of the double function in http://lukeplant.me.uk/blogmedia/monad_explanation.py,
which is presumably where you got it from. It's not being passed in
an ordinary int. int's do not have bind or return_ attributes.
 
J

John Machin

Given the following....
def double(val):
    return val.bind(lambda x: val.return_(x*2))
I get "AttributeError: 'int' object has no attribute 'bind' " when I
try to do the following
double(2)
Below is the output...
[cdalten@localhost ~]$ python
Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information..>>> def double(val):
...     return val.bind(lambda x: val.return_(x*2))
...>>> double(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in double
AttributeError: 'int' object has no attribute 'bind'
How you call it is irrelevant; you already achieved that.
The problem is that your double() function is weird. ... it fails
(quite justifiably) trying to execute 2.bind(). Just as well, because
the next problem would be 2.return_() ... int objects just don't have
such methods (as the error message pointed out).
Presumably you are trying to achieve a goal that's a bit more useful
than
def double(val):
  return val * 2
so perhaps you could explain what that goal is and someone may be able
to help.

The function is taken from the article

"Understanding Monads Via Python List Comprehensions"

found at the following url

http://lukeplant.me.uk/blog.php?id=1107301643

Near the bottom of the article, the author has

"What is the <- in the do notation then? It is simply some syntactic
sugar that allows you to define the right kind of functions easily and
painlessly. It looks very much like 'unpack this data from the monad
so I can use it', so it helps conceptually. In fact, together with the
rest of the body of the 'do' block it forms an anonymous lambda
function, and we could write our double function something pretty much
like this in Python:

def double(val):
    return val.bind(lambda x: val.return_(x*2))

(I've had to use return_ to avoid the clash with Python's 'return'
keyword). Haskell's do notation eliminates the explicit call to bind,
and the lambda, making it quite a bit easier to use. "

I just have no idea how to call double().

Near the middle of that article there's this:

"""I've done a complete example implementation of the List and Maybe
monads in Python, along with the double function as above, trying to
stay close to how it works in Haskell.""" The blue text in there is
associated with this link:
http://lukeplant.me.uk/blogmedia/monad_explanation.py

Left-click on the blue stuff and start reading and you should end up
looking at this:
"""
def double(val):
return val.bind(lambda x: val.return_(x*2))

print double(ListMonad([0,1,2]))
print double(Just(1.5))
print double(Nothing)
"""

HTH,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top