Test your skills (2006)
Author |
Message |
rizzix
|
Posted: Tue Apr 04, 2006 12:56 am Post subject: (No subject) |
|
|
Hey! Congrats and welcome to compsci!
BTW: There is no need for you to redefine any of the Prelude functions (flip, composition). I was hoping you could do it without defining any of your own functions, like twice.
The truth is, that is not the answer I was hoping for, but it is an answer I was expecting -- or one kind of answer anyway. You get 350 bits for that one
Here's my solution that's synonymous to yours (i.e worth 350bits)
Helper function: Haskell: | toPair n = (n, n) | Eta-reduction: Haskell: | f n = g (x n) (y n) -- let a = g (x n)
= (g (x n)) (y n) -- therefore, f n = (a . y) n
= ((g (x n)) . y) n -- = ((. y) a) n
= ((. y) (g (x n))) n -- therefore, f n = ((. y) (g (x n)))
= ((. y) ((g . x) n)) n
= ((. y) . g . x) n n | Therefore, Haskell: | f = (uncurry $ (. y) . g . x) . toPair |
Note that if we used your function twice, it was just a matter of: Haskell: | f = twice $ (. y) . g . x | Eitherway, this is still not what I'm looking for, for that extra 150bits. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
tez_h
|
Posted: Tue Apr 04, 2006 5:31 am Post subject: (No subject) |
|
|
rizzix wrote: Hey! Congrats and welcome to compsci!
BTW: There is no need for you to redefine any of the Prelude functions (flip, composition). I was hoping you could do it without defining any of your own functions, like twice.
Hi, and thanks. Yeah, I thought I'd just write out the definitions for ease of reference.
rizzix wrote: The truth is, that is not the answer I was hoping for, but it is an answer I was expecting -- or one kind of answer anyway. You get 350 bits for that one
<snip>
Eitherway, this is still not what I'm looking for, for that extra 150bits.
Well, I haven't quite worked out if you're expecting some ultra-clever combination of (.), ($), and flip, say, but here's one that only uses standard prelude functions, starting from near the end of my derivation:
Haskell: | f n = (flip (g.x).y) n n
= foldr1 (flip (g.x).y) (replicate 2 n)
= (foldr1 (flip (g.x).y) . replicate 2) n
f = foldr1 (flip (g.x).y) . replicate 2 |
Not pretty, and certainly point-less . Similarly, starting from near the end of your derivation:
Haskell: | f n = ((. y) . g . x) n n
= foldr1 ((. y) . g . x) (replicate 2 n)
= (foldr1 ((. y) . g . x) . replicate 2) n
f = foldr1 ((. y) . g . x) . replicate 2 |
Is that more what you're looking for?
-Tez |
|
|
|
|
|
rizzix
|
Posted: Tue Apr 04, 2006 11:51 am Post subject: (No subject) |
|
|
No. Let me give you a hint: I'm looking for a simpler solution
Another hint: (->a) |
|
|
|
|
|
tez_h
|
Posted: Wed Apr 05, 2006 4:27 am Post subject: (-> a) monad |
|
|
rizzix wrote: No. Let me give you a hint: I'm looking for a simpler solution
Another hint: (->a)
Yes, well I did see somewhere that there is a "(-> a)" monad that lambdabot (on #haskell on freenode) uses to transform point-wise expressions into point-free form. But since I haven't really done much proper haskell programming for a good few years, and googling for ' "(-> a)" monad ' turned up very little, I'm afraid I'm going to have to admit defeat here.
I'd love it if you could provide a reference or something about the (-> a) monad, or even if it has a more googlable name. Or PM me a more obvious hint!
Thanks for your time and the challenge. Now I need something to get me to learn about arrows!
-Tez |
|
|
|
|
|
rizzix
|
|
|
|
|
wtd
|
Posted: Thu Apr 20, 2006 2:34 pm Post subject: (No subject) |
|
|
C++ today.
code: | #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
char upcase(const char original);
string capitalize(const string& s);
int main(int argc, char **argv)
{
string *args = new string[argc - 1];
copy(argv + 1, argv + argc, args);
transform(args, args + argc - 1, args, capitalize);
for (int i = 0; i < argc - 1; i++)
{
cout << i << " " << args[i] << endl;
}
delete args;
return 0;
}
char upcase(const char ch)
{
return (ch >= 97 && ch <= 122) ? ch - 32 : ch;
}
string capitalize(const string& s)
{
string dup = s;
if (dup.length() > 0)
{
dup[0] = upcase(dup[0]);
}
return dup;
}
|
I have done something wrong here. Find and explain the mistake. |
|
|
|
|
|
Aziz
|
Posted: Wed Jul 12, 2006 7:48 pm Post subject: (No subject) |
|
|
I like these...get more going...I'd like to see some more java and possibly even turing? |
|
|
|
|
|
Cervantes
|
|
|
|
|
Sponsor Sponsor
|
|
|
Slaivis
|
Posted: Thu Jul 13, 2006 5:15 pm Post subject: (No subject) |
|
|
Cervantes wrote: We had one Turing TYS (of sorts) a while back: http://www.compsci.ca/v2/viewtopic.php?t=10450
That's pretty cool. Can we start it up again? I'd like to be a participant of that. |
|
|
|
|
|
Cervantes
|
Posted: Thu Jul 13, 2006 5:47 pm Post subject: (No subject) |
|
|
That one was already solved.
Feel free to dream up questions to ask. If there are no questions, there will be no TYS. |
|
|
|
|
|
Aziz
|
Posted: Thu Jul 13, 2006 7:14 pm Post subject: (No subject) |
|
|
I was going to say only the well-experienced could come up with good questions, but you know what, I'm thinkin' it's not that hard, just not so many uber-complex ones (that I never get anyways). Maybe I'll write some... |
|
|
|
|
|
wtd
|
Posted: Thu Jul 13, 2006 8:16 pm Post subject: (No subject) |
|
|
TYS isn't about "hard" questions. It isn't about questions that take a lot of "work" to solve. |
|
|
|
|
|
Aziz
|
Posted: Fri Jul 14, 2006 8:41 am Post subject: (No subject) |
|
|
Exactly. Here's an easy one:
Java: | public class hello_world {
private static String say_this_to_console;
private static final int no_of_times = 5;
public static void main (String args []) {
say_this_to_console = "Hello you dudes";
if (no_of_times <= 0) {
System. out. println(say_this_to_console );
} else {
String new_string;
new_string = "";
for (int i = 1; i <= no_of_times; i = i + 1) {
new_string = new_string + say_this_to_console + "\n";
}
System. out. println(new_string );
}
}
} |
This code compiles fine.
1) Change it all to proper convention.
2) Make it effecient (ie. there are several things in there that are too much work, I'm not asking for extreme effeciency, but plain sense) |
|
|
|
|
|
wtd
|
Posted: Fri Jul 14, 2006 11:56 am Post subject: (No subject) |
|
|
Another C++ TYS.
code: | #include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
string foo[] = {"Hello", "world"};
print_all(foo);
return 0;
} |
Make this work, such that the output is:
You may not use "sizeof" or hard-code the size of the array. In other words, it must still work if the size of the array changes.
It must also work if the type of the array is changed. For instance, if foo were an array of five integers.
Bonus points if the body of the print_all function is only one line long. |
|
|
|
|
|
Aziz
|
Posted: Fri Jul 14, 2006 2:25 pm Post subject: (No subject) |
|
|
can i try doing this in java? i think i know a way... |
|
|
|
|
|
|
|