Python from Wise Guy's Viewpoint
Brian McNamara!
gt5163b at prism.gatech.edu
Sat Oct 25 10:42:32 EDT 2003
"Marshall Spight" <mspight at dnai.com> once said:
><prunesquallor at comcast.net> wrote in message news:wuaufe52.fsf at comcast.net...
>> "Marshall Spight" <mspight at dnai.com> writes:
>> > It would be really interesting to see a small but useful example
>> > of a program that will not pass a statically typed language.
>> > It seems to me that how easy it is to generate such programs
>> > will be an interesting metric.
>>
>> Would this count?
>>
>> (defun noisy-apply (f arglist)
>> (format t "I am now about to apply ~s to ~s" f arglist)
>> (apply f arglist))
>
>Interesting, interesting. Thanks for taking me seriously!
>
>I'm trying to map this program into Java, and it's possible
...
>Anyone have any comments?
Well, in C++ you could say
template <class F, class A>
typename result_of<F(A)>::type
noisy_apply( const F& f, const A& a ) {
cout << "I am now about to apply " << f << " to " << a << endl;
return f(a);
}
These assume that both "f" and "a" work with the out-streaming operator
(<<). This is just an ad-hoc version of what would be "class Show" in
Haskell. In C++ practice, most functions aren't "showable", but many
common data types are. So the most useful version of the function would
probably be
// This version works for all "f" and all Showable "a"
template <class F, class A>
typename result_of<F(A)>::type
noisy_apply( const F& f, const A& a ) {
cout << "I am now about to apply a function with type "
<< typeid(f).name() << " to the value " << a << endl;
return f(a);
}
Again, provided that we have some notion of "class Show" in our
statically-typed language, then examples like these are easy to type.
(What dynamically-typed languages typically buy you is that every object
in the system provides some basic methods like toString(), which
eliminates the Show-able constraint that the statically-typed version
needs.)
--
Brian M. McNamara lorgon at acm.org : I am a parsing fool!
** Reduce - Reuse - Recycle ** : (Where's my medication? ;) )
More information about the Python-list
mailing list