Author |
Message |
wtd
|
Posted: Mon Oct 22, 2007 9:47 am Post subject: Some challenges... |
|
|
Unless otherwise specified, any programming language may be used. Any language that is, with the exception of Turing.
1. Write a very short program to demonstrate polymorphism (in the object-oriented sense of the word). Note: you will likely want to use some form of object-oriented programming language.
2. Demonstrate statically verified structural subtyping in a language without mandatory explicit type annotations.
3. Explain how an object-oriented programming language can lack classes, and how inheritance can work in such a language. Offer some code to demonstrate.
4. Explain why "foo" in the following is available from within "bar" in the "Baz" class. Note: Ruby.
code: | def foo
puts "foo"
end
class Baz
def bar
foo
end
end |
Also, explain why the "foo" in the class Baz overrides the previously defined "foo" when called from "bar" in Baz.
code: | def foo
puts "foo"
end
class Baz
def foo
puts "FOO!"
end
def bar
foo
end
end |
5. Consider the following Objective-C source file. Modify it such that the Bar class' "bar" method can be statically verified. Do not introduce a new class.
code: | #import <objc/Object.h>
@interface Foo : Object
- foo;
@end
@interface Bar : Object
- (void) bar;
@end
@implementation Foo
- foo
{
// This code is unimportant, do NOT add code here.
}
@end
@implementation Bar
- (void) bar
{
[[[[Foo alloc] init] foo] baz];
}
@end |
6. Translate the following to C++ from Java.
code: | interface Foo {
void bar();
} |
7. Write a tail-recursive factorial function which takes a single integer argument.
8. Going back to Objective-C, make the following work without modifying the Foo class, and without introducing a new class. The implementation of Foo (Foo.m) is considered unimportant and has no bearing on your work.
code: | // Foo.h
#import <objc/Object.h>
@interface Foo : Object
- (void) foo;
@end
// main.m
#import "Foo.h"
int main()
{
Foo *f = [[Foo alloc] init];
[f foo];
[f bar];
return 0;
} |
9. Write a simple program to demonstrate a closure.
10. Create a circular array type which can be shifted left or right such that the element on the far left will appear on the far right when the array is shifted left, and vice versa. It should be non-expandable, but the size of the array should be specified when the data structure is instantiated. The data structure must be efficiently implemented using an array. It may NOT be implemented using any sort of linked list data structure. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
PaulButler
|
Posted: Mon Oct 22, 2007 5:40 pm Post subject: RE:Some challenges... |
|
|
Good list, I will try some of these (#7 in particular has me thinking...). One question, are you ok with us posting here when we have a solution? |
|
|
|
|
|
rdrake
|
Posted: Mon Oct 22, 2007 6:05 pm Post subject: Re: Some challenges... |
|
|
Spoiler: | I think it's ok if you use the [spoiler ][ /spoiler] tags. | |
|
|
|
|
|
wtd
|
Posted: Tue Oct 23, 2007 12:43 am Post subject: RE:Some challenges... |
|
|
Note: successfully answering all questions will earn a great deal of respect from me. |
|
|
|
|
|
Zampano
|
Posted: Tue Oct 23, 2007 7:35 am Post subject: Re: Some challenges... |
|
|
And I'm there's nothing more than any of us crave than the respect of a random guy on a forum somewhere miles away (unless otherwise noted). |
|
|
|
|
|
wtd
|
Posted: Tue Oct 23, 2007 10:07 am Post subject: RE:Some challenges... |
|
|
I'm just a "random guy" on "some internet forum"?
For me, compsci.ca is more than just that. It stands for something: students coming together and taking charge of their own education, and by extension their lives.
Or maybe it's just some random internet forum, and I was bored.
Whichever. |
|
|
|
|
|
richcash
|
Posted: Tue Oct 23, 2007 3:44 pm Post subject: Re: Some challenges... |
|
|
3. Are you talking about a language like Javascript?
Spoiler: | Javascript is an object-oriented programming language with no classes. It has functions (which can have inner functions) that can be used as structures for objects (instances of the function).
Quote: function Foo() {
var bar = function() {
return "BAR"
}
this.baz = function() {
return bar() + " car"
}
}
obj = new Foo()
document.write(obj.baz())
All instances of Foo() will have a public method baz(). The private method bar() can not be accessed by objects of Foo().
Quote: function FooBar() {
this.inheritFoo = Foo
this.inheritFoo()
}
obj = new FooBar()
document.write(obj.baz())
That is inheritance. Notice you can't really say that FooBar() is inheriting everything from Foo(). If you just call Foo() inside of FooBar(), you will not get inheritance. You have to do it as above. Each object of FooBar() is inheriting everything from Foo(). Alternative syntax is :
Quote: function FooBar() {
}
FooBar.prototype = new Foo()
obj = new FooBar()
document.write(obj.baz()) | |
|
|
|
|
|
wtd
|
Posted: Tue Oct 23, 2007 3:49 pm Post subject: RE:Some challenges... |
|
|
You're pretty close, but explain it in English so I can see that you really understand it. Especially the inheritance bit. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
richcash
|
Posted: Tue Oct 23, 2007 4:51 pm Post subject: Re: Some challenges... |
|
|
Well, let's say FooBar is inheriting from Foo (as in above).
I believe it is just a property of objects. Calling a function in a function will just give the result, but calling a function belonging to an object in that object (using the 'this' keyword) will give this object everything in that function (then return the result). So, we have to make a function for each object equal to Foo and then calling this function in each object will give each object everything in the function (all functions and variables). Is this right?
The prototype is easier to explain. It basically says make every object of FooBar an instance of Foo(). 'FooBar.prototype' refers to every instance of FooBar. |
|
|
|
|
|
wtd
|
Posted: Tue Oct 23, 2007 5:07 pm Post subject: RE:Some challenges... |
|
|
You're making it sound far more complicated than it is. |
|
|
|
|
|
richcash
|
Posted: Tue Oct 23, 2007 5:37 pm Post subject: Re: Some challenges... |
|
|
Whever an object's function is called, the object receives all the functions and variables of that function.
Javascript: | function Foo() {
this.bar = function() {
this.qux = function() {
return "qux"
}
}
}
obj = new Foo()
document.write(obj.qux()) |
So to inherit all of a structure function's stuff [Foo] into all the objects of another structure function [FooBar] (as I did in my last-last post), we need to give each object of FooBar (using 'this') a function equal to Foo so it can be called and use the above property. |
|
|
|
|
|
wtd
|
Posted: Tue Oct 23, 2007 9:57 pm Post subject: RE:Some challenges... |
|
|
This still does not get at the heart of the matter of how inheritance works in such a model.
Javascript is not the easiest language to think about this in. |
|
|
|
|
|
richcash
|
Posted: Fri Oct 26, 2007 9:43 am Post subject: Re: Some challenges... |
|
|
Well, I might as well put my newly found knowledge of O'Caml polymorphic classes into use. So ...
10.
code: | class ['a] circ_array length (init_val : 'a) =
object
val array = Array.create length init_val
method set i x = array.(i) <- x
method assign new_arr = Array.iteri (fun i x -> array.(i) <- x) new_arr
method at i = array.(i)
method values = array
method shift_l = Array.iteri (fun i x ->
if i == 0 then array.(length - 1) <- x else array.(i - 1) <- x) (Array.copy array)
method shift_r = Array.iteri (fun i x ->
if i == length - 1 then array.(0) <- x else array.(i + 1) <- x) (Array.copy array)
end |
Ocaml: | let arr = new circ_array 4 "";;
arr#assign [| "a"; "b"; "c"; "d"| ];;
arr#shift_r;;
Array. iter (fun x -> print_endline x ) (arr#values );; |
|
|
|
|
|
|
wtd
|
Posted: Fri Oct 26, 2007 11:05 am Post subject: RE:Some challenges... |
|
|
Way too much copying and moving of stuff going on. There's a more efficient way to handle this. |
|
|
|
|
|
richcash
|
Posted: Fri Oct 26, 2007 12:55 pm Post subject: Re: Some challenges... |
|
|
Yeah, you're right. I threw that together too quickly. Here is a more efficient way.
code: | class ['a] circ_array init_length (init_val : 'a) =
object(this)
val last = init_length - 1
val array = Array.create init_length init_val
val mutable start = 0
method assign new_arr = Array.iteri (fun i x -> array.(i) <- x) new_arr
method at i = let index = start + i in
if index > last then array.(index - init_length) else array.(index)
method values = let rec values_ i l =
if i == init_length then l else values_ (i+1) ((this#at (last - i))::l) in
Array.of_list (values_ 0 [])
method shift_l = if start == last then start <- 0 else start <- start + 1
method shift_r = if start == 0 then start <- last else start <- start - 1
end |
|
|
|
|
|
|
|