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:
Anonymous2008-11-28 11:55
sub max($$){
my($a,$b)=@_;
$a>$b?$a:$b
}
print max 1,1.1
Name:
Anonymous2008-11-28 14:01
This is biased towards weakly-typed languages, since 1 > 1.1 may not even be possible in strongly-typed languages.
Haskell
I'll assume the caller calls fromInteger. There's already a built-in max function, but for the purposes of demonstration,
max x y | x >= y = x
| otherwise = y
main = print $ max (fromInteger 1) 1.1
Result: 1.1
Bad syntax: Fails to compile.
No comparison: Compile-time error about the missing Ord type class instance.
C++0x (theoretical, I haven't tried it)
#include <iostream>
template<typename T1, typename T2>
where LessThanComparable<T1, T2>
auto max(const T1& a, const T1& b) -> decltype(a + b) //This works by promoting the smaller type.
{
if(a < b)
return b;
return a;
}
int main() {
endl(std::cout << max(1, 1.1));
}
Result: 1.1
Bad syntax: compile-time error.
No comparison: compile-time error (comprehensible).
Ruby def max a, b; a < b ? b : a; end
puts (max 1, 1.1)
Result: 1.1
Bad syntax: syntax error.
No comparison: runtime error.
INSTANT.EXE
Just press a few buttons and it's done.
Result: 1.1
Bad syntax: not possible.
No comparison: everything is comparable.
Name:
Anonymous2008-11-28 14:22
Common Lisp
(defun mymax (a b) (max a b))
or
(defun mymax (a b) (if (a > b) a b))
Name:
Anonymous2008-11-28 14:25
>>4
Obviously I skipped writing to standard output because that's for retards.
Name:
Anonymous2008-11-28 16:22
>>1,3
They have this exact example in the Wiki about Sepplesox:
template<typename T> requires LessThanComparable<T>
const T& min(const T &x, const T &y)
{
return y < x ? y : x;
}
auto concept LessThanComparable<typename T>
{
bool operator<(T, T);
}
Name:
Anonymous2008-11-28 16:28
>>6
Surely that would suffer the same problem as >>1, because the argument types aren't the same.
Is the challenge to use some old faggy language with a weak set of libraries?
If you used a modern development framework like .Net you would just use the Math.Max function. That only works for numeric types though. If you want to compare non-numeric or dissimilar types you just implement the iComparable or iComparable<T> interfaces and maybe a TypeConverter.
public class Compare<T> where T : IComparable<T>
{
public static T Max(T a, T b)
{
return (a.CompareTo(b)) ? b : a;
}
}
Call it like:
Compare.Max<double>(1, 1.1);
Although int and double are not the same type, it will compile and run just fine. int implements a TypeConverter to convert from int to double for the comparison. So this function can be used for any type implementing the standard iComparable interface and if they are dissimilar types, one type needs a TypeConver implemented to convert to the other type.
Why anyone continues to use the abortion that is C++ for anything except legacy maintenance is beyond any sane developer.
Name:
Anonymous2008-11-28 18:16
Fuck, I meant Compare<double>.Max(1, 1.1);
Name:
Anonymous2008-11-28 18:23
>>9
There's no challenge in doing this exercise in your omg abstracted languages. Try doing this in x86, you vagabond.
>>14
Had to clean up the code a bit:
public class Compare<T> where T : IComparable<T>
{
public static T Max(T a, T b)
{
return (a.CompareTo(b) < 0) ? b : a;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Compare<double>.Max(1, 1.1));
}
}
Prints 1.1
Name:
Anonymous2008-11-28 20:12
>>14
Had to clean up the code a bit:
public class Compare<T> where T : IComparable<T>
{
public static T Max(T a, T b)
{
return (a.CompareTo(b) < 0) ? b : a;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Compare<double>.Max(1, 1.1));
}
}
Prints 1.1
int main(void){
puts(str(bigger(1, 1.1)));
return 0;
}
Result: 1.1
Bad syntax: syntax error
No comparison: not possible
Name:
Anonymous2008-11-29 1:05
Language: x86 Asm
Compiler: Human Brain
Results: 1.1
Bad syntax: Assembler complains
No comparison: Bits are bits, it'll compare them anyway
Name:
Anonymous2008-11-29 4:46
>>17
Clever but that's not what I meant.
Your supposed to compare instances of the same type with a supplied comparison function/operator/whatever. Yours doesn't compare an integer 1 with a 2 properly.
Name:
Anonymous2008-11-29 7:17
>>19 Implement a single function that takes two arguments and returns the bigger of the two.
if that's not what you meant, you should have said what you meant instead.
Name:
Anonymous2008-11-29 7:25
>>20
Are you blind? I've supplied two code examples. tl;dr, huh?
Name:
Anonymous2008-11-29 7:47
>>8 $ gcc -std=c++0x max.cpp
max.cpp: In function ‘int main()’:
max.cpp:12: error: no matching function for call to ‘max(int, double)’
Am I doing something wrong here?
>>21
you said you wanted the bigger of the two. your code examples are both broken, and therefore not very useful for trying to figure out what you wanted to do.
The maxOfFloats, maxOfInts, convertToFloat, and customized printf function is left as an exercise to the reader.
prints: 1.1
broken syntax: You get laughed at.
no comparison: They are compared as floats.
Name:
Faggot OP2008-11-29 12:23
>>4
This one is interesting. How do I define a comparison function for non-primitive types so that I can use it with types other than numbers?
>>23
How is it broken? They both compile and run fine. If you can't run a compiler properly then at least write an example in something that won't tax your pair of brain cells too much (like FIOC). and stop trolling.
Thank you sir. For extra credit I would love to see someone implement a single function like that (not using managed C++) that can correctly return on these inputs.
Console.WriteLine(Compare<double>.Max(1, 1.1));
Console.WriteLine(Compare<double>.Max("1", 1.1));
Console.WriteLine(Compare<double>.Max(1, "1.1"));
Console.WriteLine(Compare<double>.Max("1", "1.1"));
All of those will return 1.1 calling the same function with no modifications.
Name:
Anonymous2008-11-29 17:52
static class Compare<T>
where T : IComparable<T>
{
public static T Max<A1, A2>(A1 a1, A2 a2)
where A1 : IConvertible
where A2 : IConvertible
{
T t1 = (T)a1.ToType(typeof(T), System.Globalization.CultureInfo.InvariantCulture);
T t2 = (T)a2.ToType(typeof(T), System.Globalization.CultureInfo.InvariantCulture);
return t1.CompareTo(t2) < 0 ? t2 : t1;
}
}
Holy fuck, so many amateurs on this fucking board. Can;t even use Google. Insist on posting first to make themselves look like an idiot.
When you grow up and write programs that actually people actually use, you will find out some of those people do not live in the same fucking country as you. Some data is formatted differently depending if you are an American or a terrorist(Rest of the World).
What date is this: 01/02/2009?
If you are an American you would say January 2nd. If you are a terrorist you would say it February 1st. So when converting and comparing types that contain data that is formatted in a way specific to a region, you need to take in to account how faggy that region is. If you are developing on some faggy legacy language and/or framework, it probably does not have these conventions built in. .Net does because it is not faggy.
But I think >>31 should have used System.Threading.Thread.CurrentThread.CurrentCulture.
>>37 But I think >>31 should have used System.Threading.Thread.CurrentThread.CurrentCulture.
Yeah, if he was a fucking retard. Globalization is there to make things more compatible, not to give your program a chance to blow up whenever it's run in a different region.
InvariantCulture is for things that the user doesn't see (such as configuration files, perhaps), CurrentUICulture for localizing string and bitmap resources, and CurrentCulture for formatting dates and numbers displayed to the user.
No, you are a fucking idiot. To expand and spoon feed you more on the example I already provided, lets use that function to see which is later, January 2nd 2009 or February 1st 2009.
If I wanted to compare some user inputted string it would be called like this:
Console.WriteLine(Compare<DateTime>.Max("01/02/2009", "02/01/2009"));
And the output is correct: 02/01/2009
A britfag would use the same inputs, and get the same result as is. To a britfag, the result reads that January 2nd is later than February first. This of course is fucking wrong, and you somehow think that makes it more compatible.
Using System.Threading.Thread.CurrentThread.CurrentCulture, will correct the comparison (assuming the thread is set with something like: System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
Not only will the program behave as expected for peoples all over the world, it is a simple change that will not blow anything fucking up. There is no chance that would introduce a bug. It fixes the bug of the function not accounting for the format of different data types in different cultures.
The fact I initially gave a very example, you didn't understand it, then claimed it would 'blow up' despite contending that the globalization feature makes apps more compatible, ad the best way to use it is to not use it (format provider is required in the call to ToType, and specifying InvariantCulture means don't apply any formatting). As in the simple date example, formatting is important.
In summary, you are an idiot faggot.
Name:
Anonymous2008-11-30 20:35
>>41
Why are you getting so upset over a small programming question? Did mommy and daddy not hug you enough when you were a child?
What the nigger fuck do you think you are trying to talk about? 2009 is a year. It is a year in both the commonly used mm/dd/yyyy and dd/mm/yyyy formats.
At no point does the data 01/02/2009 have anything to do with there been 2009 days in January. The date 01/02/2009 is a valid date in either of those common formats.
Only an incredible master faggot would come up with something so stupid.
I know what that does and why it is there. It's just that System.Globalization.CultureInfo.InvariantCulture and System.Threading.Thread.CurrentThread.CurrentCulture are probably even a step below NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, which you don't seem to notice in your RAGE.
As I ALREADY FUCKING SAID, the average person is not going to know what that is if it is up to them to work with dates in an application. Even if your gave an input hint with something like yyyy-mm-dd they would still fuck it up, people are fucking stupid.
Now if you are an expert loonix programer you would require only the ISO date. You would give no input hint. You would not document this. Someone will have to find that fact on some obscure outdated wikipage.
>>49
I guess you're enjoying writing programs for idiots (judging by your posts' contents). We'll enjoy our loonix and obscure dates. Have an ice day.
Name:
Anonymous2008-12-01 0:35
Nah, I am just a good programmer. I write apps people can use. Not lazy shit because I am too shit eating reatrded to handle some data formats. Expecially when the solution is so fucking simple that a nigger like you can't even understand it.
Name:
Anonymous2008-12-01 0:39
>>49
Actually they will find in the man page that all dates and times are to be POSIX time. Anyone who wouldn't like that won't have made it far enough into my program or the docs to even know about it. It's called defensive programming, learn it.
Enter the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds:
Defensive loonix programming ftw!
Name:
Anonymous2008-12-01 12:52
>>57
What exactly is your point? No program prompts user for that. Stop spreading around your stupidity and just go back to /g/ already.
Name:
OP2008-12-01 16:15
>>58
He has a point.
You shouldn't store and manipulate a date in it's original format.
That's retarded. You should always store it as an integer and compare it as that. Conversion should be left to the UI.
>>31
and >>36
are fucking retarded ways to implement a max function.
Why the fuck should the max function care about type conversion, it's not it's fucking job. I've given my examples a single type parameter for a reason.
Name:
Anonymous2008-12-01 16:44
>>59
What's retarded is not knowing the difference between its and it's.
Name:
Anonymous2008-12-01 17:31
>>59 I've given my examples a single type parameter for a reason.
So that means I can't compare objects of different types, even if operator< is defined for them?
>You shouldn't store and manipulate a date in it's original format. That's retarded. You should always store it as an integer and compare it as that.
Speaking of retarded, a retard would not understand that datetime types from any decent library take care of storing date times as an integer internally. If you feel compelled to work with datetimes as an integer and not a datetype type, you are a fucking retard. Did you think there might be a reason that datetime types are fairly ubiquitous?
>are fucking retarded ways to implement a max function.
And to further prove to you that you are indeed, a retard; the original posed asked to "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." Then the very example used in the OP was for 2 numbers of different types.
So already, the challenge is to return the maximum value of 2 things that might not be the same type, but could be compared. So type conversion is at the very heart of the challenge, retard.
The you go on to retardedly say: I've given my examples a single type parameter for a reason.
A retard like yourself does not understand that >>31 and >>36 do take a single fucking type parameter.
So now that you understand the challenge was a generic max function that can handle different types that can be compared, you should understand that those 2 actually did it fucking right, and you are too much of a retard to understand that.
Which brings me back to the whole why the fuck did I bring up dates and strings. See, you were too retarded to follow, so more spoon feeding of a retard.
If you need to gather a date from a user, it is not uncommon or unreasonable to ask for that date as a string that they input in to the app. Any decent datetime type can create the datetype from a string input. So given the perfectly valid generic function provided for .net, one can certainly compare 2 strings of formatted date information. The only drawback was it expected dates than an American (and some other cultures) would put in. The fix to allow for a user to easily enter date information according to the standards of their culture was quick and simple, so it is unreasonable to force the user to conform to your ignorant and inflexible single standard just because you are lazy and stupid. Also, datetime type is a simple example for this, there are other cases where this would be used also.
Now you could be butt-hurt and say that the data should be corrected before passed to the function and blah blah lazy ignorant bullshit. But then once more, you would be missing the point like the little faggy retard you are.
If the function is going to be generic and useful and do type conversion, it needs to handle the format of the data it is converting. I could care less if you are too stupid to understand the example is just an example and an apt one. The example is a simple way to demonstrate further concerns, but the attempt was to condence it to something as simple as a date, which we thought retards like yourself could understand.
Now you stupid little faggy retard, if you still do not understand, DO NOT FUCKING POST. You will make yourself look even stupider.
Speaking of retarded, a retard would not understand that datetime types from any decent library take care of storing date times as an integer internally.
Yes, you demonstrated that perfectly with your examples. You compare two objects of that internal, library type. Not a fucking number and string and convert them in a max function!
Then the very example used in the OP was for 2 numbers of different types.
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?
A retard like yourself does not understand that >>31 and >>36 do take a single fucking type parameter. >>36 in fact, does not. But you've already shown you're an angered EXPERT PROGRAMMER and don't need to think through what you've read.
Now you could be butt-hurt and say that the data should be corrected before passed to the function and blah blah lazy ignorant bullshit. But then once more, you would be missing the point like the little faggy retard you are. ihbt
If the function is going to be generic and useful and do type conversion, it needs to handle the format of the data it is converting
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.
You have entirely missed the point of the exercise you homosexual cumdupster.
>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.
* The reinterpret_cast built-in keyword safely type-casts the type of the r-value to the type specified in the template specifier. reinterpret_cast can be used to perform the following conversions:
+ std::string to numerical primitive
+ IEEE 754 binary floating point to integer
+ integer to IEEE 754 binary floating point
Name:
Anonymous2008-12-02 20:52
>>73
C++ is probably the ugliest programming language I've ever seen
Name:
Anonymous2008-12-02 21:13
>>77
You're mom is probably the ugliest programming language I've seen.
Name:
Anonymous2008-12-02 21:20
>>78
You're dad is probably the sexiest programming language I've cummed inside of.
int main( int argc, char *argv[] ) {
std::cout << reinterpret_cast<int>( std::string( "1" ) ) << std::endl;
return 0;
}
[b][/b]> g++ test.cpp
test.cpp: In function 'int main(int, char**)':
test.cpp:5: error: invalid cast from type 'std::string' to type 'int'
>>70
Excuse my C++ but it looks like you don't understand english.
Here's a tested example, my "simple mind" has come up with. #include <cstdlib>
#include <ctime>
#include <iostream>
>>88
I like how you didn't instanciate an Abelson. That is how it should be.
Name:
Anonymous2008-12-03 17:07
>>1
>Post the compiler you used, the code and the result.
Haskell, GHC.
myMax a b = if a > b then a else b
main = putStrLn $ show $ myMax (fromInteger 1) 1.1
>Write what will happen if the max function is called with
>broken syntax (I'm looking at you C macros).
This isn't a macro, so there's no problem.
>Write what will happen if the objects of a type can't
>be compared.
Haskell's type system ensures that both arguments must be instances of the Ord typeclass, so it's impossible to pass incomparable arguments.
myMax :: Ord a => a -> a -> a
Name:
Anonymous2008-12-03 17:57
New exercise:
In a programming language of your choice, implement a function max(a,b), which returns the maximum of a and b. The types of a and b may not necessarily be the same, or even comparable. The type returned by the function must be the type of the maximum value passed in as a parameter, whatever it may be.
Name:
Anonymous2008-12-03 18:50
>>93
I think you've just excluded every statically-typed language by requiring that the function have two return types depending upon the run-time arguments.
Awww, you had to use a common base type of the types, how Comp Sci 102. Its cute. You even implemented it like I said, in some unfounded attempt to disagree with me.
How about something where you compare 2 types that do not share the same base type in common, but do contain data that can be compared.
Such as, once again, in the simple example of comparing a datetime value to a string that contains data that is in the correct format for a datetime. We already saw how easy this is in .Net.
Make a generic function to compare niggers and people. The nigger class would not be derived from a class like Person or any other class that describes a human, of course. But they can be compared because niggers are always less than people. 3/5 as much (at most) if I remember correctly.
Name:
Anonymous2008-12-03 20:45
void * max(void * a, void * b, void * compare_function) {
/* compare function needs to return a void pointer to either
a or b */
return compare_function(a,b);
}
Name:
Anonymous2008-12-03 20:52
>>96
Lovely. Now all you have to do is write the code for compare_function.
Name:
Anonymous2008-12-03 20:55
>>97
Why? How it works depends on how the user wants it to work. A sample implementation serves no purpose.
Name:
Anonymous2008-12-03 20:58
>>98
Okay, then describe how YOU want it to work, as a user. You can type your description as valid C code which performs the function outlined in >>93
// return max of two entities irrespective of type
// a and b are the entities, sizeof_a and sizeof_b are their size in bytes
// returns -1 if a is larger, 1 if b is larger, 0 if invalid pointers
int max(const void * a, const int sizeof_a, const void * b, const int sizeof_b) {
// bounce null pointers
if((a=NULL)||(b=NULL)){return 0;}
// handle cases where entities are 0 length
if((sizeof_a==0)||(sizeof_b!=0)){return 1;}
if((sizeof_a!=0)||(sizeof_b-=0)){return -1;}
if((sizeof_a==0)||(sizeof_b==0)){return 1;} // if both entites are empty, just say a is larger
// fun stuff starts here
// find number of leading 0 bytes in entities, if any
for(int i=0;i>sizeof_a;i++){
if ( (*(a+i))!=0 ){break;}
}
for(int j=0;j>sizeof_b;j++){
if ( (*(b+j))!=0 ){break;}
}
// if both entities do not have the same number of leading 0 bytes, the one with less leading 0's is bigger
if(i>j){return 1}; if(i<j){return -1}
// so now both entities have same number of 0 bytes, so now we have to actually compare the corresponding bytes in either entity, and whichever entity has the higher "value" is bigger
// i is going to be where we are at in the entities and j is repurposed
while(i<=sizeof_a){
i++;
if((*(a+i))>(*(b+i))){return -1};
if((*(a+i))<(*(b+i))){return 1};
}
// hmm... at this point the entities are the same
// so just say a is bigger and call it a day
return -1;
}
>>103
experiencing the pleasure of being cummed inside
Name:
Anonymous2008-12-04 0:57
>>102
Okay, that's it you just angered an expert programmer here. Yes hi, okay, hello. So suppose you passed in "0000000000001" and 100 to this function. It would seem that your code will give an undefined result. Additionally, your comparisons seem to be based solely on the size of the data type, and not so much as the logical magnitude of the values being compared.
The heart of the problem is type conversion, being able to convert input types to a type which can be comparable. The supplementary problem relates to some sort of reflection/RTTI mechanism available in the language, in order to return the correct type based upon the input.
Name:
Anonymous2008-12-04 4:24
>>105
Reflection/RTTI would be the easy way out, but I gathered from the OP's statement that RTTI wouldn't be available.
If you have no type information available, and no way to get it, the byte-level content and size of the data is really all you got to go on.
Ok, so I guess the proper thing to do is to compare each corresponding byte of a and b, even if one is longer than the other, until the smaller entity's size is reached. At that point, all other things being equal, the larger entity is the maximum. Correct?
Another solution would be to apply heuristic functions to try to discover the type, like the unix file command except for data types. Such a function would need to be able to return multiple possible types together with a probability that the data is indeed that type. Detecting every possible type would be shit slow but that's probably the best way, assuming no reflection/RTTI.
Name:
OP2008-12-04 7:59
>>95 How about something where you compare 2 types that do not share the same base type in common, but do contain data that can be compared.
Like the Abelson from my example can be compared to a Sussman?
You could make a generic algorithm that will pummel any type into submission but the results will be meaningless in almost all cases. Therefore, I think, it is a genuinely good idea to assume that if a type can be compared to another type it will explicitly state that in it's interface (like the Abelson class). You could also compare bits that consist the data of an object to bits of another object of a different type but such a comparison is useless in most cases, unless you're interested in run-time profiling.
You've expired your entertainment value , troll.
>>106
RTTI and reflection are allowed. You're allowed to use each and every feature of your language of choice. I didn't mean to bias this in favor of static languages. I want to see what tools can each language provide for writing code similar to >>88.
Name:
Anonymous2008-12-04 8:05
>>107 type it will explicitly state that in it's interface it's
instance Convertable Int Float where
convert = fromIntegral
myMax :: (Convertable a b, Ord b) => a -> b -> b
myMax a b = let ba = convert a in
if ba > b then ba else b
main =
do let x = 1 :: Int
let y = 1.1 :: Float
putStrLn $ show $ myMax x y
The myMax can compare anything that is Convertable, as long as the second argument can also be Ordered. Adding new instances to make other types Convertable should be trivial.
Name:
Anonymous2008-12-04 22:10
>>110
Your code doesn't meet >>93's requirements. Also, "convertable" is not a word, lrn2english.
Name:
Anonymous2008-12-04 23:10
>>111
"In a programming language of your choice, implement a function max(a,b), which returns the maximum of a and b. The types of a and b may not necessarily be the same, or even comparable. The type returned by the function must be the type of the maximum value passed in as a parameter, whatever it may be."
OK, this takes arguments of two types, a and b; a and b don't have to be comparable, but a must be convertible into b, and b must be able to compare. It will return either a value of type a if that argument was bigger, or of type b if that was bigger.
As >>94 said, you probably cannot exactly follow >>93's intent with a static type system. However, here, with Haskell's type classes, I've been able to achieve something very close.
class Convertible a b where
convert :: a -> b
instance Convertible Int Float where
convert = fromIntegral
myMax :: (Convertible a b, Ord b) => a -> b -> Either a b
myMax a b = let ba = convert a in
if ba > b then Left a else Right b
main =
do let x = 1 :: Int
let y = 1.1 :: Float
putStrLn $ show $ myMax x y
Name:
Anonymous2008-12-04 23:12
THE PLEASURE OF BEING CUMMED INSIDE
Name:
Anonymous2008-12-05 0:47
>>112
Now thank /prog/ for the constructive criticism, you fucking ingrate.
Name:
Anonymous2008-12-05 1:12
>>111
Maybe English isn't his mother language, you insufferable fool.
The space of the keys of why fags and dykes should be burned off the face by the headmaster for my ridikulus ideas Guess he saw everything I am supposed to display the prime integers and also the ones you feel that it was modified When.
Name:
Anonymous2009-03-06 13:42
The Abelson will return to his Earthly form end the NET platform on which modern developers are creating network services Enterprises and win much more badly than the JVM citation needed u sup u.