How do I call the double() function?

John Machin sjmachin at lexicon.net
Sat Mar 21 11:42:16 EDT 2009


On Mar 22, 2:21 am, grocery_stocker <cdal... at gmail.com> wrote:
> On Mar 21, 8:11 am, John Machin <sjmac... at lexicon.net> wrote:
>
>
>
> > On Mar 22, 1:55 am, grocery_stocker <cdal... at gmail.com> wrote:
>
> > > 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 at 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





More information about the Python-list mailing list