Python and Ruby
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Mon Feb 1 01:05:17 EST 2010
On Sun, 31 Jan 2010 20:22:36 -0800, Paul Rubin wrote:
> Terry Reedy <tjreedy at udel.edu> writes:
>> Three of you gave essentially identical answers, but I still do not see
>> how given something like
>>
>> def f(): return 1
>>
>> I differentiate between 'function object at address xxx' and 'int 1'
>> objects.
>
> In the languages they are talking about, there is no such thing as a
> function with no args. A function is closer to a mathematical function,
> i.e. a mapping from one type to another, so every function has an arg.
Suppose I have a function that queries a website http://guessmyname.com
for a list of popular names and returns the most popular name on the
list. Obviously this name will change from time to time, so I can't just
get it once and treat the result as a constant.
In a non-functional language, I'd write it something like this:
def get_popular_name():
URL = 'http://guessmyname.com'
data = fetch(URL)
names = parse(data)
name = choose(names, 1)
return name
name = get_popular_name() # call the function with no argument
f = decorate(get_popular_name) # treat the function as a 1st class object
How would Haskell coders write it? Something like this?
def get_popular_name(url):
data = fetch url
names = parse data
name = choose name 1
return name
name = get_popular_name 'http://guessmyname.com' # call the function
f = decorate get_popular_name # treat the function as a 1st class object
But now you're needlessly having the caller, rather than the function,
remember an implementation detail of the get_popular_name function. Since
the argument couldn't be anything different, I'd immediately think of
applying partial:
get_popular_name = partial get_popular_name 'http://guessmyname.com'
but now how do I call the new function?
Is this where you say "Monads" and everyone's eyes glaze over?
--
Steven
More information about the Python-list
mailing list