PDA

View Full Version : Polymorphism!!!!! programmers jargon.



darkservent
09-04-05, 19:50
Well have a technical telephone interview on thursday and theres one thing I can never do is to explain Polymorphism.

If you were asked to explain what it polymorphism is then wot sentence would you use to explain it?

All the books and sites I go to have like looooooooooooooooong explanations which I cant summerise into a short sentences.

MkVenner
09-04-05, 20:01
aint polymorphism....shapeshifting?

numb
09-04-05, 20:25
Polymorphism is when you overload a method in a class, or override a method in derived class. [Personally I think of it more when I override a method, but according to a few books I've read, overloading should be considered polymorphism aswell]

edit: You could use a real world example, of say wanting to change the ring function of a bell phone to buzz instead of ringing bell. You could inherit the original bell phone, alter its circuitry so it activates the buzzer instead of the bell when it rings and that should be considered polymorphism.

Nidhogg
09-04-05, 20:54
Hmm, that's not exactly clear. It's also only one kind of polymorphism (known as ad-hoc polymorphism) which at the end of the day isn't really even polymorphism at all!

E.g. If you overload the "+" operator so it can add two integers, two reals or append two strings you could say that the operator is polymorphic but in reality it only appears that way to the programmer using the operator but under the covers the implementor of the operator has to write separate implementations for each type.

There are a bunch of other kinds of polymorphism but they really only apply in functional programming languages like Standard ML or Haskell (hateful languages, I don't care what anyone says).

I suspect that it's ad-hoc polymorphism that you'll need to know about which is what I described first. Think of writing code that can be generalised to work with many types of things such as that "+" operator and you won't go far wrong.

N

Mighty Max
09-04-05, 21:05
(hateful languages, I don't care what anyone says).


Those programs are the poems under all source code.

Take the fibonacci:


fib 0 = 0
fib 1 = 1
fib n = fib (n - 2) + fib (n - 1)


or a quicksort:



qsort [] = []
qsort (x:xs) = qsort [y | y <- xs, y < x] ++ [x] ++ qsort [y | y <- xs, y >= x]


They are so beautifully because of their pureness. I mean, no jumps, no loops, pure declarations.

They however get painfull to read if you take functions on functions of functions :p


:edit: On the topic (a bit) Haskell & co doesnt support polymorphism. They only work on one defined type. But in exchange you can declare functions that convert functions. Best example therefor is the curry & uncurry functions

darkservent
09-04-05, 21:07
Ah ok. So when I write something and I have 2 constructors of the same name but with different parameters is that also considered polymorphic?

numb
09-04-05, 21:11
Ah ok. So when I write something and I have 2 constructors of the same name but with different parameters is that also considered polymorphic?

Apparently it is, because the class's construction function has more than one form. That is my understanding anyway (which may be incorrect, but I normally dont have too many troubles at job interviews).

Nidhogg
09-04-05, 21:12
I accept that the idea is to describe the result and not how you achieve it but try implementing any reasonably non-trivial real-world program rather than copy/pasting something out of a "Functional programs R grate!" website and you soon realise how much of a PITA they are. ;)

Q: How many Prolog programmers does it take to change a lightbulb?
A: No

N

/edit - Haskell doesn't support polymorphism? Check out this (http://mudshark.cit.gu.edu.au/~andrewrock/p1.98.2/11-chapter.pdf) document for a great introduction to polymorphism using Haskell as an example.

Mighty Max
09-04-05, 21:33
Oh well, no :p

It works only on the defined datatype. You cant declare i.e.

add' :: Int -> Int -> Int

and

add' :: a -> [a] -> [a]

(just try it out)

It even cant support "add (a,b)" and "add a b" as "(a,b)" and "a b" would be considered different, non matching types.

What you can do in Haskell is to provide a conversation between those types.
As curry,uncurry, map & co do.

for the second example the call would be then

map add' Int [Int]

Once the type is defined of the operation you can't declare another type its working for. That Haskell handles all types if you dont declare a special ones is another story :p (Because it does not evaluate any expression till it is needed)

CMaster
09-04-05, 21:58
Q: How many Prolog programmers does it take to change a lightbulb?
A: No

I actually found that funny...
Especiially as during one class, the guy next to me told it to "exit" with the response "no".