Python from Wise Guy's Viewpoint
Fergus Henderson
fjh at cs.mu.oz.au
Mon Nov 3 08:25:49 EST 2003
Fergus Henderson <fjh at cs.mu.oz.au> writes:
>Anyway, regarding how to write this example in a statically typed
>language: you can do this in a quite straight-forward manner,
>by just keeping a separate table of employees.
>For example, here it is in Java.
And in case you didn't like the type declarations and downcast that you
need in Java, here it is in Mercury.
:- module employed.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module map, string, std_util.
default_company = "constanz-inc".
:- type employee ---> some [Obj]
employee(object::Obj, salary::int, company::string).
hire(Obj, Salary, !Employees) :-
hire(Obj, Salary, default_company, !Employees).
hire(Obj, Salary, Company, !Employees) :-
set(!.Employees, Obj, 'new employee'(Obj, Salary, Company),
!:Employees).
fire(Obj, !Employees) :-
delete(!.Employees, Obj, !:Employees).
:- type person ---> person(name::string).
test_employed(!.Employees) -->
{ Joe = person("joe") },
print("-> hire joe"), nl,
{ hire(Joe, 60000, !Employees) },
print("name: " ++ Joe^name), nl,
print("class: " ++ type_name(type_of(Joe))), nl,
print("employed: " ++ (if !.Employees `contains` Joe
then "yes" else "no")), nl,
print("company: " ++
!.Employees^det_elem(Joe)^company), nl,
print("salary: "),
print(!.Employees^det_elem(Joe)^salary), nl,
print("-> fire joe"), nl,
{ fire(Joe, !Employees) },
(if {!.Employees `contains` Joe} then
print("joe is still employed."), nl
else
print("joe is not employed anymore."), nl
).
main --> { init(Employees) }, test_employed(Employees).
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
More information about the Python-list
mailing list