Again on polymorphism.
From: seguso (look_at_in.signature)
Date: 04/19/04
- Previous message: seguso: "Re: Question about polymorphism (maybe)"
- Next in thread: Nick Wedd: "Re: Again on polymorphism."
- Reply: Nick Wedd: "Re: Again on polymorphism."
- Reply: Lex Spoon: "Re: Again on polymorphism."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 19 Apr 2004 20:28:53 GMT
Hello again, :-)
At first, it seems you can mimick polymorphism (duck typing) in prolog this
way:
% this is a polymorphic predicate. It can be applied to any X for which
% width/2 and height/2 are defined.
area( X, A):-
width(X, W),
height(X, H),
A is W*H.
width(ship( W, _, _Passengers), W).
height(ship( _, H, _Passengers), H).
width(plane( W, _), W).
height(plane( _, H), H).
Now I can have a list of intermixed planes and ships, and still apply area/2
to all members of the list:
maplist(area, [plane(32,3), ship(2,3,4)], Areas).
But this approach cannot be used when the polymorphic predicate must modify
the argument! I mean:
% walk/2: I want walk/2 to be polymorphic, i.e. to apply to any X for
% which speed/2 and position/2 are defined.
walk(X, NewX):-
speed(X, S),
position(X, P),
NewPosition is P + S,
NewX = ????
I need to call walk/2 both with X = ship(3,4,5) and with X = plane(3,4).
What I need to say is "let NewX = a copy of X where the position has been
replaced by NewPosition".
Is there a way to do that?
Thanks a lot! ^_^
Maurizio
-- Best Regards, Maurizio Colucci Please remove the uppercase letters "S,P,A,M": seSgPuAsMo.forever@tin.it
- Previous message: seguso: "Re: Question about polymorphism (maybe)"
- Next in thread: Nick Wedd: "Re: Again on polymorphism."
- Reply: Nick Wedd: "Re: Again on polymorphism."
- Reply: Lex Spoon: "Re: Again on polymorphism."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]