From: linnig@skvax1.csc.ti.com (Mike Linnig)
Subject: RE: Dynamic Typing in Ada
Date: 10 Mar 89 16:49:27 GMT [thread overview]
Message-ID: <8903101703.AA04518@ti.com> (raw)
John Gateley's (Gateley@tilde.csc.ti.com) challenge was to implement the
following lisp construct in Ada...
----------------------------------------------------------------------
-- Scheme CL
-- (define print (defun print (x)
-- (lambda (x) (typecase x
-- (cond (integer 1)
-- ((integer? x) 1) (real 2)
-- ((real? x) 2) (complex 3)
-- ((complex? x) 3) (vector 4)))
-- ((vector? x) 4))))
----------------------------------------------------------------------
package dynamic_types is
TYPE real IS NEW integer; -- not how we would really do it
TYPE complex IS NEW integer; -- but good enuf for an example.
TYPE vector IS NEW integer;
Type Dynamic_Type_options is (integer_t,real_t,complex_t,vector_t);
type Dynamic_Type(which: Dynamic_Type_options := integer_t) IS record
CASE which IS
WHEN integer_t => I: integer;
WHEN real_t => R: real;
WHEN complex_t => C: complex;
WHEN vector_t => V: vector;
END CASE;
END RECORD;
FUNCTION print(x: Dynamic_Type) return integer;
END dynamic_types;
----------------------------------------------------------------------
WITH text_io;
PACKAGE BODY dynamic_types IS
FUNCTION print(x: Dynamic_Type) return integer is
BEGIN
CASE x.which IS
WHEN integer_t => return (1);
WHEN real_t => return (2);
WHEN complex_t => return (3);
WHEN vector_t => return (4);
END CASE;
END print;
END Dynamic_Types;
----------------------------------------------------------------------
WITH dynamic_types,text_io;
USE dynamic_types;
PROCEDURE lisp_test IS
PACKAGE int_io IS NEW text_io.integer_io(integer); -- I hate this.
tmp1 : dynamic_type := (which=>real_t, r=> 3); -- init to real type
BEGIN
text_io.put("the type number of tmp1 is :");
int_io.put(print(tmp1));
text_io.new_line;
tmp1 := (which=>complex_t, c => 99);
text_io.put("the NEW type number of tmp1 is :");
int_io.put(print(tmp1));
text_io.new_line;
END lisp_test;
----------------------- SAMPLE OUTPUT ---------------------------
the type number of tmp1 is : 2
the NEW type number of tmp1 is : 3
----------------------------------------------------------------------
I think this answers John Gateley's (Gateley@tilde.csc.ti.com) dynamic
typing challenge. I didn't understand the first challenge; if it involved
something similar to The C Language construct allowing pointers to
procedures (and calling them) that can be handled too.
Mike Linnig,
Texas Instruments
Geez, we work for the same company!
next reply other threads:[~1989-03-10 16:49 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1989-03-10 16:49 Mike Linnig [this message]
1989-03-19 21:52 ` Dynamic Typing in Ada Som Hane
1989-03-24 18:42 ` Scott Moody
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox