Contrary Accumulator Generator
started 2/16/2003; 10:15:23 AM - last post 2/19/2003; 9:32:22 AM
|
|
Isaac Gouy - Contrary Accumulator Generator
2/16/2003; 10:15:23 AM (reads: 401, responses: 5)
|
|
Is it implicit in the Accumulator Generator problem description, that the solution will be more awkward if the language is statically typed? (The function parameter types are unknown at compile time).
What would be a contrasting problem?
A problem that would be awkward if the language is dynamically typed. Presumably straight-forward solutions would exploit the presence of static typing in the same way that Accumulator Generator exploits it's absence.
|
|
Dan Shappir - Re: Contrary Accumulator Generator
2/17/2003; 1:38:30 AM (reads: 420, responses: 1)
|
|
I don't think the awkwardness in some of the implementations of the AG have to do with static typing. The main issue IMO is with proper support for scopes. Proper scope support has nothing to do with the kind of typing you use. If there is any relation it may have to do with the fact that statically typed languages often stress code performance more than dynamically typed languages, and proper scope support can degrade performance.
As for your contrasting problem, in static languages (that support generics) it is very is to define a general container that can hold only a specific type of object. If you use a dynamically typed language you have to resort to a container that can hold any type of object and must perform a test at runtime. This testing code, as well as the considerations of what to do if the test fails, add a layer of complexity.
|
|
Matt Hellige - Re: Contrary Accumulator Generator
2/17/2003; 7:56:43 AM (reads: 424, responses: 0)
|
|
As for your contrasting problem, in static languages (that support generics) it is very is to define a general container that can hold only a specific type of object. If you use a dynamically typed language you have to resort to a container that can hold any type of object and must perform a test at runtime. This testing code, as well as the considerations of what to do if the test fails, add a layer of complexity.
Actually, I think overloading is the key here, since the problem requires that a single addition operator be used for any type of number. In fact, as another post observed, the problem is poorly specified, since few languages allow arbitrary mixing of numeric types (nor should they, necessarily). For example, suppose I do something like (pseudocode):
f = foo 3
f 5.3
f Complex (3, 2i)
f Rational (45, 123)
Should this really work?
If I were to specify the problem, I'd say foo takes two arguments: an initial value, and an incrementor function. Then foo is fully specified and can be easily implemented in a language with Hindley-Milner types:
fun foo initial add =
let val store = ref initial
in fn n => (store := add (!store, n); !store) end;
Then foo has type 'a -> ('a * 'b -> 'a) -> 'b -> 'a and we can use it however we like.
|
|
Isaac Gouy - Re: Contrary Accumulator Generator
2/18/2003; 2:03:44 PM (reads: 348, responses: 0)
|
|
static typing
One day I will remember how inadequate this phrase is.
We could list half-a-dozen language features that are required for a straightforward implementation.
contrasting problem
You've both helped to change my mind ;-)
Thinking about a contrasting problem was wrongheaded.
Much more interesting would be a language that allowed a straightforward implemention of Accumulator Generator, and provided compile-time guarantees (so we can check that the function parameters are only ever numbers for which + is reasonable).
|
|
Dan Shappir - Re: Contrary Accumulator Generator
2/19/2003; 3:45:02 AM (reads: 334, responses: 1)
|
|
so we can check that the function parameters are only ever numbers for which + is reasonable
I'm not sure what you mean by reasonable in this context. Would an accumulator for strings (via concatenation) be reasonable in this context? For matrices?
Any statically typed language could provide such compile-time guarantee (provided that it does support the + operator for the types you want (and even more so if it allows overloading of this operator for user defined types).
If C++ was extended to provide proper scopes (along with function nesting and anonymous functions), a safe implementation in that language would have been short and trivial (IMO there is nothing in the C++ language that inherently precludes such an extension, aside from performance considerations).
|
|
Isaac Gouy - Re: Contrary Accumulator Generator
2/19/2003; 9:32:22 AM (reads: 358, responses: 0)
|
|
reasonable in this context
I was just trying to be vague enough to avoid a math debate ;-)
strings? matrices? Do you consider them to be "numbers"?
"Works for any numeric type-- i.e. can take both ints and floats and returns functions that can take both ints and floats."
Let's stop there - ints and floats - and see what the solutions would be like.
Any statically typed language could...
and allow a straightforward implemention?
We could imagine that this exercise was tailor-made to point out that some languages, sometimes force programmers to jump through hoops ;-)
Let's be charitable and regard this as an exercise in Comparative Programming Languages, rather than language advocacy.
if X was extended...
Interesting as step 2 (what do we need to change/add so that we can have a straightforward solution using language X). Step 2 for OCaml.
And also an admission that, at present, we don't have that straightforward solution.
|
|
|
|