Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Generic algorithms

Name: Anonymous 2008-11-28 11:31

Implement a single function that takes two arguments and returns the bigger of the two. Assume you don't know the type of the  arguments and you don't know if the types can be compared. Assume that if a type can be compared, it will always be implemented by following a single standard. Use the latest standard of your language. Write the simplest program
that will pass an integer 1(one) and a float 1.1(one point one) into the function and write the result to standard output.

Post the compiler you used, the code and the result.
Write what will happen if the max function is called with broken syntax (I'm looking at you C macros).
Write what will happen if the objects of a type can't be compared.

I'll start with C and C++.
Compiler: gcc 4.3.2 20081105 (Red Hat 4.3.2-7)
#include <iostream>
template<class F>
F& max(F& a, F& b) { return (a < b) ? b : a; }
int main() { std::cout << max(1, 1.1) << '\n'; }

Results: Compile time error:
max.cpp: In function ‘int main()’:
max.cpp:6: error: no matching function for call to ‘max(int, double)’

Bad syntax: Standard compile-time error
No comparison: Standard compile-time error about undefined operator<

#define max(a, b) (less(a, b) ? b : a)
#include <stdio.h>
int less(int a, int b) { return a < b; }
int main() { printf("%f\n", max(1, 1.1)); }

Result: Bad output
1.000000
Bad syntax: Depends on the error in the syntax. Can either compile and cause undefined behaviour or fail at compile time with strange syntax errors.
No comparison: Standard compile-time error about an undefined function.

Name: Anonymous 2008-12-02 15:09

>Because the exercise was meant to show how a given language handles a case when the function is called with different types. Does it do a implicit conversion? Does it just gobble it up and doesn't care? Does it rape you with 5 pages of template errors?

If you need the details of a language/compilers abilities of coercion or lack thereof, you do not need to write fucking code to find out. RTFM.

>Not a fucking number and string and convert them in a max function!

A simple mind like yours is not able to understand the implications of the simple examples given. It is simple to compare an int and a string that contains number data for the purposes of demonstration. They are 2 dissimilar types that can be compared, the whole fucking point of this exercise. You have limited yourself to primitive types that are easily coerced because you can't seem to understand further implications of that for other types.

>ihbt

No, you were going down the path that you would NEVER store dates as strings. The point isn't to show that dates as string is a good thing, it is a simple demonstration of type conversion. So, for the purposes of example, taking user input and creating a datetime and then comparing those is fucking pointless for this exercise.

>It should not. The point of a generic algorithm is that it will work on all types which implement an interface that that algorithm requires. It doesn't care if it's apples, oranges or a bunch of retarded motherfuckers like you, because it's GENERIC.

Wow, this is obviously way over your head. If an apple can't be an orange, then we need to compare them with something they have in common, like that they are fruit. So apple and orange need to be converted to fruit. Not all fruits will need to. Granny Smith Apple might have the ability to be compared to McIntosh Apple, and a comparison of fruit might not be accurate enough in that scenario.

But as you said, generic functions do care about type because the type needs to implement something in common. Then you say it doesn't care about type. Oh lordy.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List