[Haskell-cafe] Re: Increasing number of parameters

Jacek Generowicz jacek.generowicz at cern.ch
Fri Oct 15 06:46:04 EDT 2010


On 2010 Oct 15, at 11:53, Kevin Jardine wrote:

> Jacek,
>
> I haven't been following this thread in any detail, so I apologise if
> I misunderstand your goal,

My goal (in this thread, at least) is to become a better Haskell  
programmer, rather than to actually write any specific program. Yes,  
there are specific goals cited as examples, but the overall purpose is  
the journey, rather than the destination: I want to learn to walk and  
to run, rather than to get anywhere, just yet.

> but the ctm function in the polyToMonoid
> library (which maps its parameters to any specified monoid) appears to
> work in just this way.

Yes, I noticed your earlier announcement. Yes, I recognized that it's  
pertinent to my last message. Yes, I've stored it in my (rapidly  
growing) list of things that Haskell Cafe has thrown at me that I  
should look into more deeply :-)

But my current short-term goal is to understand the plumbing in a  
function that Brandon supplied, and to acquire the ability to write  
this kind of function myself in my sleep, with my hands tied behind my  
back, while the walls are falling all around me. At the moment I'm not  
managing to write it at all :-(

> It keeps consuming parameters until you hand it to the trm function to
> deliver the final result. More documentation here:

Sounds a bit like the scheme I use for curried functions in Python.  
Though in Python I also have the option of calling the function with  
zero arguments to indicate termination, rather than terminating more  
explicitly by giving it to a terminate function.

(Curried functions in Python? Can you tell that there's a Haskell  
programmer dying to get out ? :-)

I've thrown in an example at the end, in case anybody is interested.

> http://hackage.haskell.org/packages/archive/polyToMonoid/0.1/doc/html/Data-PolyToMonoid.html

It's already in my bookmarks, but thanks for taking the time to bring  
it to my attention.


=======

from functools import partial

def curry(fn):
     """Function decorator. Curries its argument. The curried version
     collects all positional and keyword arguments it is given, until
     it is called with an empty argument list, at which point it
     applies the function to all the collected arguments."""

     def curried_function(*args, **kwds):
         if not (args or kwds):
             return fn()
         else:
             it = partial(fn, *args, **kwds)
             try:
                 it.__name__ = fn.__name__
             except AttributeError:
                 pass
             return curry(it)

     try:
         curried_function.__name__ = fn.__name__ + ' (curried)'
     except AttributeError:
         pass

     curried_function.fn = fn
     return curried_function


@curry
def collect(*args, **kwds):
     return "I've collected: %s %s" % (args, kwds)

print collect                # <function collect (curried) at 0x712db0>
print collect(1)             # <function collect (curried) at 0x712d30>
print collect(1)(2,3,c=4)    # <function collect (curried) at 0x712bf0>
print collect(1)(2,3,c=4)()  # I've collected: (1, 2, 3) {'c': 4}



More information about the Haskell-Cafe mailing list